-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.dylan
More file actions
212 lines (187 loc) · 6.92 KB
/
utils.dylan
File metadata and controls
212 lines (187 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
Module: %testworks
Synopsis: Utilities and code that needs to be loaded early.
Copyright: Original Code is Copyright (c) 1995-2004 Functional Objects, Inc.
All rights reserved.
License: See License.txt in this distribution for details.
Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
define function add-times
(sec1 :: <integer>, usec1 :: <integer>, sec2 :: <integer>, usec2 :: <integer>)
=> (sec :: <integer>, usec :: <integer>)
let sec = sec1 + sec2;
let usec = usec1 + usec2;
if (usec >= 1000000)
usec := usec - 1000000;
sec1 := sec1 + 1;
end if;
values(sec, usec)
end function add-times;
//// Tags
define class <tag> (<object>)
constant slot tag-name :: <string>, init-keyword: name:;
constant slot tag-negated? :: <boolean>, init-keyword: negated?:;
end;
define method make-tag
(tag :: <tag>) => (tag :: <tag>)
tag
end;
define method make-tag
(spec :: <string>) => (tag :: <tag>)
let negated? = (~empty?(spec) & spec[0] == '-');
let name = copy-sequence(spec, start: negated? & 1 | 0);
if (empty?(name))
error("Invalid tag: %=", spec);
end;
make(<tag>, name: name, negated?: negated?)
end method make-tag;
define method print-object
(tag :: <tag>, stream :: <stream>) => ()
format(stream, "#<tag %s%s>", tag.tag-negated? & "-" | "", tag.tag-name);
end;
define function parse-tags
(specs :: <sequence> /* of <string> */)
=> (tags :: <sequence> /* of <tag> */)
map(make-tag, specs)
end;
// If tags match, run the test.
define generic tags-match?
(requested-tags :: <sequence>, component :: <component>)
=> (bool :: <boolean>);
define method tags-match?
(requested-tags :: <sequence>, component :: <component>)
=> (bool :: <boolean>)
#t
end;
define method tags-match?
(requested-tags :: <sequence>, test :: <runnable>)
=> (bool :: <boolean>)
local method match (negated?)
block (return)
for (rtag in requested-tags)
if (rtag.tag-negated? = negated?)
for (ctag in test.test-tags)
if (ctag.tag-name = rtag.tag-name)
return(#t)
end;
end;
end;
end;
end block
end method match;
let negative-rtags? = any?(tag-negated?, requested-tags);
let positive-rtags? = any?(complement(tag-negated?), requested-tags);
block (return)
// Order matters here. Negative tags take precedence.
negative-rtags? & match(#t) & return(#f);
positive-rtags? & return(match(#f));
#t
end block
end method tags-match?;
// Might want to put an extended version of this in the io:format module.
define function format-bytes
(bytes :: <integer>) => (string :: <string>)
let (divisor, units) = case
bytes <= 1024 =>
values(1, "B");
bytes <= ash(1, 20) =>
values(1024, "KiB");
otherwise =>
values(ash(1, 20), "MiB");
// Need more bits in our integers...
end;
concatenate(integer-to-string(round/(bytes, divisor)), units)
end function format-bytes;
define function capitalize
(string :: <string>) => (_ :: <string>)
concatenate(as-uppercase(copy-sequence(string, end: 1)),
copy-sequence(string, start: 1))
end function;
// For --progress and --report=full
define thread variable *indent* :: <string> = "";
define constant $indent-step :: <string> = " ";
define function next-indent () => (indent :: <string>)
concatenate(*indent*, $indent-step)
end function;
define function default-runner-temp-directory () => (dir :: <directory-locator>)
let dylan = os/environment-variable("DYLAN");
let base = if (dylan)
as(<directory-locator>, dylan)
else
fs/working-directory()
end;
let dated-dir
= concatenate(os/login-name() | "unknown",
"-",
date/format("%Y%m%d-%H%M%S", date/now()));
// We could just include milliseconds (%F) in the date format below but currently
// <date> milliseconds are always zero, at least on Unix.
// https://github.com/dylan-lang/testworks/issues/199
iterate loop (i = 1)
let uniquifier = format-to-string("%s.%d", dated-dir, i);
let dir = subdirectory-locator(base, "_test", uniquifier);
if (block ()
fs/file-exists?(dir)
exception (ex :: fs/<file-system-error>)
#f
end)
loop(i + 1)
else
dir
end
end
end function;
// Return a temporary directory unique to the current test or benchmark.
define function test-temp-directory () => (d :: false-or(<directory-locator>))
if (instance?(*component*, <runnable>))
let safe-name = map(method (c)
if (c == '\\' | c == '/') '_' else c end
end,
component-name(*component*));
let test-directory
= subdirectory-locator(runner-temp-directory(*runner*), 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;
// For output to the main output stream for the test run. That is, this output is never
// redirected to the _captured-stdout.txt file in a test's temp directory.
define method test-output
(format-string :: <string>, #rest format-args) => ()
let stream = if (*runner*)
runner-output-stream(*runner*)
else
*standard-output*
end;
with-stream-locked (stream)
apply(format, stream, format-string, format-args);
force-output(stream);
end;
end method;
define constant $captured-output-filename :: <string> = "_captured-stdout.txt";
define variable *output-captured?* :: <boolean> = #f;
define function write-captured-output (output :: <string>)
block ()
let file = file-locator(test-temp-directory(), $captured-output-filename);
write-test-file(file, contents: output);
*output-captured?* := #t;
exception (err :: <error>)
test-output("ERROR writing to test's output file: %s\n%s\n", err, output);
end;
end function;