Skip to content

Commit

Permalink
Simplified testing with runner-based test.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlalis committed Jan 24, 2024
1 parent aedf966 commit 744c6c9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
26 changes: 5 additions & 21 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,11 @@ jobs:
uses: dlang-community/setup-dlang@v1
with:
compiler: ${{ matrix.compiler }}

- name: "Example: hello-world"
working-directory: examples
run: dub -q build --single hello-world.d

- name: "Example: using-headers"
working-directory: examples
run: dub -q build --single using-headers.d

- name: "Example: file-upload"
- name: Test Examples
working-directory: examples
run: dub -q build --single file-upload.d

- name: "Example: handler-testing"
run: rdmd runner.d test
- name: Clean Examples
working-directory: examples
run: dub test --single handler-testing.d

- name: "Example: static-content-server"
working-directory: examples/static-content-server
run: dub -q build --single content_server.d

- name: "Example: websocket"
working-directory: examples/websocket
run: dub -q build --single server.d
run: rdmd runner.d clean
33 changes: 33 additions & 0 deletions examples/runner.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env rdmd
module examples.runner;

import std.stdio;
import std.process;
import std.conv;
Expand All @@ -13,6 +15,7 @@ import core.thread;
interface Example {
string name() const;
Pid run(string[] args) const;
Pid test() const;
string[] requiredFiles() const;
}

Expand Down Expand Up @@ -51,6 +54,18 @@ class DubSingleFileExample : Example {
);
}

Pid test() const {
return spawnProcess(
["dub", "build", "--single", filename],
std.stdio.stdin,
std.stdio.stdout,
std.stdio.stderr,
null,
Config.none,
workingDir
);
}

string[] requiredFiles() const {
if (workingDir == ".") {
return [filename];
Expand All @@ -77,6 +92,10 @@ class DubSingleFileUnitTestExample : Example {
);
}

Pid test() const {
return run([]);
}

string[] requiredFiles() const {
return [filename];
}
Expand All @@ -86,6 +105,7 @@ const Example[] EXAMPLES = [
new DubSingleFileExample("hello-world.d"),
new DubSingleFileExample("file-upload.d"),
new DubSingleFileExample("using-headers.d"),
new DubSingleFileExample("path-handler.d"),
new DubSingleFileExample("static-content-server", "content_server.d"),
new DubSingleFileExample("websocket", "server.d"),
new DubSingleFileUnitTestExample("handler-testing.d")
Expand All @@ -102,6 +122,8 @@ int main(string[] args) {
return 0;
} else if (args.length > 1 && toLower(args[1]) == "run") {
return runExamples(args[2..$]);
} else if (args.length > 1 && toLower(args[1]) == "test") {
return testExamples();
}
writeln("Nothing to run.");
return 0;
Expand Down Expand Up @@ -192,3 +214,14 @@ int runExamples(string[] args) {
}
}
}

int testExamples() {
foreach (example; EXAMPLES) {
int exitCode = example.test().wait();
if (exitCode != 0) {
writefln!"Example %s failed with exit code %d."(example.name, exitCode);
return exitCode;
}
}
return 0;
}

0 comments on commit 744c6c9

Please sign in to comment.