util/test-util.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Helpers for the builtin tests (#[test] functions) of the compiler sources | ||
| 4 | |||
| 5 | // Std imports | ||
| 6 | import "std/data/vector"; | ||
| 7 | import "std/io/dir"; | ||
| 8 | import "std/io/file"; | ||
| 9 | import "std/io/filepath"; | ||
| 10 | import "std/os/filesystem"; | ||
| 11 | import "std/os/os"; | ||
| 12 | import "std/os/process"; | ||
| 13 | import "std/time/time"; | ||
| 14 | import "std/type/type-conversion"; | ||
| 15 | |||
| 16 | // Number of temp dirs, the tests created so far. Keeps the dirs apart, if two of them are created within the same microsecond | ||
| 17 | unsigned long CREATED_TEST_DIRS = 0l; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Create a new, empty dir in the temp dir. The test has to remove it again via removeTestDir() | ||
| 21 | * | ||
| 22 | * @param prefix Prefix of the dir name | ||
| 23 | * @return Path of the created dir | ||
| 24 | */ | ||
| 25 | 5 | public f<FilePath> makeUniqueTestDir(const string prefix) { | |
| 26 | 5 | unsigned long dirIdx = CREATED_TEST_DIRS++; | |
| 27 | 5 | const String dirName = String(prefix) + "-" + toString(getCurrentMicros()) + "-" + toString(cast<long>(dirIdx)); | |
| 28 | 5 | result = FilePath(getTempDir()) / dirName.getRaw(); | |
| 29 | // Pass the path as is, because toGenericString() would convert the separators of the path in place | ||
| 30 | 5 | assert mkDirs(result.toString(), MODE_ALL_RWX) == 0; | |
| 31 | 5 | } | |
| 32 | |||
| 33 | /** | ||
| 34 | * Delete the given dir with all its contents. Std has no function for that, so the tools of the OS are used | ||
| 35 | * | ||
| 36 | * @param dir Dir to remove | ||
| 37 | */ | ||
| 38 | 5 | public p removeTestDir(const FilePath& dir) { | |
| 39 | 5 | Vector<String> args; | |
| 40 | − | if isWindows() { // LCOV_EXCL_LINE - coverage runs on Linux | |
| 41 | − | args.pushBack(String("/c")); // LCOV_EXCL_LINE - coverage runs on Linux | |
| 42 | − | args.pushBack(String("rmdir")); // LCOV_EXCL_LINE - coverage runs on Linux | |
| 43 | − | args.pushBack(String("/s")); // LCOV_EXCL_LINE - coverage runs on Linux | |
| 44 | − | args.pushBack(String("/q")); // LCOV_EXCL_LINE - coverage runs on Linux | |
| 45 | − | args.pushBack(String(dir.toNativeString())); // LCOV_EXCL_LINE - coverage runs on Linux | |
| 46 | − | const Result<ProcessResult> _ = runProcessCaptured(String("cmd"), args); // LCOV_EXCL_LINE - coverage runs on Linux | |
| 47 | − | } else { // LCOV_EXCL_LINE - coverage runs on Linux | |
| 48 | 5 | args.pushBack(String("-rf")); | |
| 49 | 5 | args.pushBack(String(dir.toString())); | |
| 50 | 5 | const Result<ProcessResult> _ = runProcessCaptured(String("rm"), args); | |
| 51 | 5 | } | |
| 52 | 5 | } | |
| 53 | |||
| 54 | /** | ||
| 55 | * Create a dir and all of its missing parent dirs | ||
| 56 | * | ||
| 57 | * @param dir Dir to create | ||
| 58 | */ | ||
| 59 | 4 | public p createTestDirs(const FilePath& dir) { | |
| 60 | // Pass the path as is, because toGenericString() would convert the separators of the caller's path in place | ||
| 61 | 4 | assert mkDirs(dir.toString(), MODE_ALL_RWX) == 0; | |
| 62 | 4 | } | |
| 63 | |||
| 64 | /** | ||
| 65 | * Write the given content to a file at the given path | ||
| 66 | * | ||
| 67 | * @param path File path | ||
| 68 | * @param content File content | ||
| 69 | */ | ||
| 70 | 11 | public p writeTestFile(const FilePath& path, const string content) { | |
| 71 | 11 | const Result<bool> writeResult = writeFile(path.toString(), content); | |
| 72 | 11 | assert writeResult.isOk(); | |
| 73 | 11 | } | |
| 74 | |||
| 75 | /** | ||
| 76 | * Create a symlink to a dir. Creating symlinks requires special privileges on Windows, so this is only supported on Unix | ||
| 77 | * | ||
| 78 | * @param target Dir, the symlink points to | ||
| 79 | * @param link Path of the symlink | ||
| 80 | * @return Created or not | ||
| 81 | */ | ||
| 82 | 1 | public f<bool> createTestDirSymlink(const FilePath& target, const FilePath& link) { | |
| 83 | 1 | if isWindows() { | |
| 84 | ✗ | return false; | |
| 85 | } | ||
| 86 | 1 | Vector<String> args; | |
| 87 | 1 | args.pushBack(String("-s")); | |
| 88 | 1 | args.pushBack(String(target.toString())); | |
| 89 | 1 | args.pushBack(String(link.toString())); | |
| 90 | 1 | const Result<ProcessResult> processResult = runProcessCaptured(String("ln"), args); | |
| 91 | 1 | if !processResult.isOk() { | |
| 92 | ✗ | return false; | |
| 93 | } | ||
| 94 | 1 | const ProcessResult& process = processResult.unwrap(); | |
| 95 | 2 | return process.exitCode == 0; | |
| 96 | 1 | } | |
| 97 |