Skip to content

Commit

Permalink
Move some code to utils.dylan
Browse files Browse the repository at this point in the history
  • Loading branch information
cgay committed Oct 13, 2023
1 parent 4afda47 commit 9c3981b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
45 changes: 0 additions & 45 deletions run.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,6 @@ Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
// test/benchmark execution and while setup/teardown code is running.
define thread variable *component* :: false-or(<component>) = #f;

// Return a temporary directory unique to the current test or benchmark. The
// directory is created the first time this is called for a given test.
// The directory is _test/<user>-<yyyymmdd-hhmmss>/<full-test-name>/, relative
// to ${DYLAN}/, if defined, or relative to fs/working-directory() otherwise.
define function test-temp-directory () => (d :: false-or(<directory-locator>))
if (instance?(*component*, <runnable>))
let dylan = os/environment-variable("DYLAN");
let base = if (dylan)
as(<directory-locator>, dylan)
else
fs/working-directory()
end;
let uniquifier
= format-to-string("%s-%s", os/login-name() | "unknown",
date/format("%Y%m%d-%H%M%S", date/now()));
let safe-name = map(method (c)
if (c == '\\' | c == '/') '_' else c end
end,
full-component-name(*component*));
let test-directory
= subdirectory-locator(base, "_test", uniquifier, safe-name);
fs/ensure-directories-exist(test-directory);
test-directory
end
end function;

// Create a file in the current test's temp directory with the given contents.
// If the file already exists an error is signaled. `filename` is assumed to be
// a relative pathname; if it contains the path separator, subdirectories are
// created. File contents may be provided with the `contents` parameter,
// otherwise an empty file is created. Returns the full, absolute file path as
// a `<file-locator>`.
define function write-test-file
(filename :: fs/<pathname>, #key contents :: <string> = "")
=> (full-pathname :: <file-locator>)
let locator = merge-locators(as(<file-locator>, filename),
test-temp-directory());
fs/ensure-directories-exist(locator);
fs/with-open-file (stream = locator,
direction: #"output", if-exists: #"signal")
write(stream, contents);
end;
locator
end function;

define inline function debug-failures?
() => (debug-failures? :: <boolean>)
debug-runner?(*runner*) == #t
Expand Down
46 changes: 46 additions & 0 deletions utils.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,49 @@ define function format-bytes
end;
concatenate(integer-to-string(round/(bytes, divisor)), units)
end function format-bytes;

// Return a temporary directory unique to the current test or benchmark. The
// directory is created the first time this is called for a given test.
// The directory is _test/<user>-<yyyymmdd-hhmmss>/<full-test-name>/, relative
// to ${DYLAN}/, if defined, or relative to fs/working-directory() otherwise.
define function test-temp-directory () => (d :: false-or(<directory-locator>))
if (instance?(*component*, <runnable>))
let dylan = os/environment-variable("DYLAN");
let base = if (dylan)
as(<directory-locator>, dylan)
else
fs/working-directory()
end;
let uniquifier
= format-to-string("%s-%s", os/login-name() | "unknown",
date/format("%Y%m%d-%H%M%S", date/now()));
let safe-name = map(method (c)
if (c == '\\' | c == '/') '_' else c end
end,
full-component-name(*component*));
let test-directory
= subdirectory-locator(base, "_test", uniquifier, safe-name);
fs/ensure-directories-exist(test-directory);
test-directory
end
end function;

// Create a file in the current test's temp directory with the given contents.
// If the file already exists an error is signaled. `filename` is assumed to be
// a relative pathname; if it contains the path separator, subdirectories are
// created. File contents may be provided with the `contents` parameter,
// otherwise an empty file is created. Returns the full, absolute file path as
// a `<file-locator>`.
define function write-test-file
(filename :: fs/<pathname>, #key contents :: <string> = "")
=> (full-pathname :: <file-locator>)
let locator = merge-locators(as(<file-locator>, filename),
test-temp-directory());
fs/ensure-directories-exist(locator);
fs/with-open-file (stream = locator,
direction: #"output", if-exists: #"signal")
write(stream, contents);
end;
locator
end function;

0 comments on commit 9c3981b

Please sign in to comment.