Skip to content

Commit

Permalink
Add test.with_temp_dir test func
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiojr committed Oct 3, 2024
1 parent 8e47b74 commit cba5d0b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
48 changes: 33 additions & 15 deletions lib/test.risor
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ failed_is_fatal := func(b) {
}

_last_error := nil
// internal use only
// internal use only, to self-test this library
failed_raises := func() {
__raises = true
}
// internal use only
// internal use only, to self-test this library
failed_does_not_raise := func() {
__raises = false
}
Expand All @@ -35,19 +35,17 @@ func exit_if_fatal() {
//
// desc: the test description
func print_ok(desc) {
print(_green.sprintf('%s:', " ok"), desc)
if desc != "" {
print(_green.sprintf('%s:', " ok"), desc)
}
}

// Print a message in red.
//
// desc: the test description
func print_fail(desc) {
if __raises {
if _last_error == nil {
error(_raised_error)
}
error(_last_error)
_last_error = nil
error(desc)
}
print(_red.sprintf('%s:', "fail"), desc)
exit_if_fatal()
Expand Down Expand Up @@ -79,18 +77,20 @@ func equals(desc, o1, o2) {
// fn: the function that should raise an error
// err: the error that should be raised
func raises(desc, fn, err="") {
happened := false
try(fn, func(e) {
happened = true
_last_error = e
processed := false
o := try(fn, func(e) {
processed = true
if e != err{
print_fail(desc + '(expected: {err}, got: {e})')
} else {
print_ok(desc)
}
}, func(e) {
print_ok(desc)
})
if !happened {
error(sprintf("%s %s", _red.sprintf('%s:', "fail"), desc))

if o == nil && !processed {
print(sprintf("%s %s", _red.sprintf('%s:', "fail"), desc))
}
}

Expand Down Expand Up @@ -150,16 +150,23 @@ func stdout_matches(desc, cmd, reg) {
}
}

// Assert that a path is a directory.
//
// desc: the test description
// path: the path to check
func is_dir(desc, path) {
ok := try(func() { f := os.stat(path); f.is_dir }, false)
if ok {
print_ok(desc)
} else {
//error(raised_error)
print_fail(desc)
}
}

// Assert that a path is a file.
//
// desc: the test description
// path: the path to check
func is_file(desc, path) {
ok := try(func() { f := os.stat(path); !f.is_dir }, false)
if ok {
Expand All @@ -169,3 +176,14 @@ func is_file(desc, path) {
//error(raised_error)
}
}

// Create a temporary directory and run a function with it.
// The directory created is automatically removed after the function is run.
//
// fn: the function to run
func with_temp_dir(fn) {
tmp_dir := filepath.join(os.temp_dir(), uuid())
os.mkdir_all(tmp_dir)
defer os.remove_all(tmp_dir)
return fn(tmp_dir)
}
9 changes: 9 additions & 0 deletions tests/test.risor
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ test.is_file('{tmp_file} is a file', tmp_file)
test.raises('{os.temp_dir()} is not a file', func() {
test.is_file("", os.temp_dir())
})

tdir := ""
test.with_temp_dir(func(temp_dir) {
tdir = temp_dir
test.is_dir('temp dir {temp_dir} exists', temp_dir)
})
test.raises('{tdir} does not exist', func() {
test.is_dir("", tdir)
})

0 comments on commit cba5d0b

Please sign in to comment.