From 1afb6b8f8dc0c8e689e35d9ab3410be7485c08c7 Mon Sep 17 00:00:00 2001 From: kucaahbe <93769+kucaahbe@users.noreply.github.com> Date: Sun, 11 Feb 2024 22:47:45 +0200 Subject: [PATCH] ci: add silly test runner --- dub.sdl | 4 ++ dub.selections.json | 1 + source/config_parser.d | 28 ++++++++------ source/path.d | 21 ++++++----- source/symlink.d | 83 ++++++++++++++++++++++++------------------ source/test_dir.d | 21 +++++++++++ 6 files changed, 101 insertions(+), 57 deletions(-) create mode 100644 source/test_dir.d diff --git a/dub.sdl b/dub.sdl index af7032c..9125f97 100644 --- a/dub.sdl +++ b/dub.sdl @@ -13,3 +13,7 @@ dependency "sdlite" version="~>1.2.0" configuration "application" { targetType "executable" } + +configuration "unittest" { + dependency "silly" version="~>1.1.1" +} diff --git a/dub.selections.json b/dub.selections.json index 18f71f9..3fa7d0d 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -2,6 +2,7 @@ "fileVersion": 1, "versions": { "sdlite": "1.2.0", + "silly": "1.1.1", "taggedalgebraic": "0.11.22" } } diff --git a/source/config_parser.d b/source/config_parser.d index 007c802..84d6f7a 100644 --- a/source/config_parser.d +++ b/source/config_parser.d @@ -92,12 +92,13 @@ unittest import std.exception; import std.regex; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); auto config = Config(); - auto configFilePath = ".test/app/install.sdl"; + auto configFilePath = testDir ~ "/app/install.sdl"; auto configFileContent = q"CFG install { # parse error here: @@ -117,13 +118,14 @@ unittest { import std.file; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); immutable auto cwd = getcwd(); auto config = Config(); - auto configFilePath = ".test/app/install.sdl"; + auto configFilePath = testDir ~ "/app/install.sdl"; auto configFileContent = q"CFG install { ln `a` `b` @@ -142,12 +144,13 @@ unittest { import std.file; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); auto config = Config(); - auto configFilePath = ".test/app/install.sdl"; + auto configFilePath = testDir ~ "/app/install.sdl"; auto configFileContent = q"CFG install { ln "too" "many" "params" @@ -187,12 +190,13 @@ unittest import std.exception; import std.regex; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); auto config = Config(); - auto path = Path(".test/app/install.sdl"); + auto path = Path(testDir ~ "/app/install.sdl"); ubyte[] not_utf_data = [207, 250, 237]; TestFile(path.absolute, not_utf_data).create; diff --git a/source/path.d b/source/path.d index 33b1703..aa506f1 100644 --- a/source/path.d +++ b/source/path.d @@ -55,11 +55,12 @@ struct Path import std.file; import std.process; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - immutable auto file_path = ".test/app/source"; + immutable auto file_path = testDir ~ "/app/source"; const auto path = Path(file_path); assert(!path.exists); @@ -99,11 +100,12 @@ struct Path import std.file; import std.process; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - immutable auto dir_path = ".test/app"; + immutable auto dir_path = testDir ~ "/app"; const auto path = Path(dir_path); assert(!path.isDir); @@ -125,11 +127,12 @@ struct Path import std.file; import std.process; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - immutable auto dir_path = ".test/app"; + immutable auto dir_path = testDir ~ "/app"; const auto path = Path(dir_path); assert(!path.isSymlink); diff --git a/source/symlink.d b/source/symlink.d index 9239795..3fb03c6 100644 --- a/source/symlink.d +++ b/source/symlink.d @@ -27,9 +27,13 @@ struct Symlink { unittest { import std.file; + import test_dir; - immutable auto srcPath = ".test/app/source"; - immutable auto destPath = ".test/home/destination"; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); + + immutable auto srcPath = testDir ~ "/app/source"; + immutable auto destPath = testDir ~ "/home/destination"; immutable auto cwd = getcwd(); const auto link = Symlink(srcPath, destPath); @@ -56,12 +60,13 @@ struct Symlink { { import std.file; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; TestFile(srcPath, "content").create; TestFile(destPath).create; @@ -105,12 +110,13 @@ struct Symlink { import std.file; import std.path; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; TestFile(srcPath, "content").create; TestFile(destPath).create; @@ -131,12 +137,13 @@ struct Symlink { import std.path; import std.regex; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; immutable auto cwd = getcwd(); TestFile(srcPath, "src content").create; @@ -161,25 +168,26 @@ struct Symlink { { import std.file; import std.path; - import test_file; import std.exception: collectExceptionMsg; + import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); immutable auto cwd = getcwd(); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; TestFile(srcPath, "content").create; - // only create .test/ + // only create testDir ~ "/home" TestFile(destPath.dirName).create; assert(destPath.dirName.dirName.isDir); auto link = Symlink(srcPath, destPath); immutable auto exceptionMsg = collectExceptionMsg!FileException(link.link); - immutable auto expectedExceptionMsg = cwd ~ "/.test/home" ~ ": directory does not exist"; + immutable auto expectedExceptionMsg = cwd ~ '/' ~ testDir ~ "/home" ~ ": directory does not exist"; assert(exceptionMsg == expectedExceptionMsg, exceptionMsg ~ " != " ~ expectedExceptionMsg); } @@ -191,14 +199,15 @@ struct Symlink { { import std.file; import std.path; - import test_file; import std.exception: collectExceptionMsg; + import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; immutable auto cwd = getcwd(); TestFile(srcPath, "src content").create; @@ -220,14 +229,15 @@ struct Symlink { { import std.file; import std.path; - import test_file; import std.exception: collectExceptionMsg; + import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; immutable auto cwd = getcwd(); TestFile(destPath).create; @@ -236,7 +246,7 @@ struct Symlink { auto link = Symlink(srcPath, destPath); immutable auto exceptionMsg = collectExceptionMsg!FileException(link.link); - immutable auto expectedExceptionMsg = cwd ~ "/.test/app/source" ~ ": file or directory does not exist"; + immutable auto expectedExceptionMsg = cwd ~ '/' ~ testDir ~ "/app/source" ~ ": file or directory does not exist"; assert(exceptionMsg == expectedExceptionMsg, exceptionMsg ~ " != " ~ expectedExceptionMsg); } @@ -257,12 +267,13 @@ struct Symlink { import std.file; import std.path; import test_file; + import test_dir; - if (".test".exists) ".test".rmdirRecurse; - scope(exit) if (".test".exists) ".test".rmdirRecurse; + auto testDir = setupTestDir(__FILE__, __LINE__); + scope(exit) removeTestDir(testDir); - auto srcPath = ".test/app/source"; - auto destPath = ".test/home/destination"; + auto srcPath = testDir ~ "/app/source"; + auto destPath = testDir ~ "/home/destination"; immutable auto cwd = getcwd(); TestFile(srcPath, "src content").create; diff --git a/source/test_dir.d b/source/test_dir.d new file mode 100644 index 0000000..d0d2b71 --- /dev/null +++ b/source/test_dir.d @@ -0,0 +1,21 @@ +version(unittest) +{ + import std.file; + import std.path; + import std.string; + import std.conv; + + string setupTestDir(string filename, uint line) + { + auto dir = ".test_" ~ filename.replace('/', '_') ~ "_" ~ to!string(line); + + if (dir.exists) dir.rmdirRecurse; + + return dir; + } + + void removeTestDir(string dir) + { + if (dir.exists) dir.rmdirRecurse; + } +}