src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
102 / 104
98.1%
Branches:
27 / 38, 42 excl
71.1%
Functions:
8 / 9
88.9%

spice_packages/manifest/manifest-file.spice
Line Branch Exec Source
1 // Std imports
2 import "std/data/vector";
3 import "std/io/dir";
4 import "std/io/file";
5 import "std/io/filepath";
6 import "std/os/filesystem";
7 import "std/time/time";
8 import "std/type/type-conversion";
9
10 // Own imports
11 import "manifest";
12
13 // ---------------------------------------------------------------------------------------------------------------------
14 // Reading, writing and finding spice.toml on disk. The rest of the package does no I/O and is in manifest.spice.
15 // ---------------------------------------------------------------------------------------------------------------------
16
17 /**
18 * Read and validate a spice.toml from disk. `label` names the file in the messages, which are appended to `errors`.
19 * Returns false if it cannot be read or is invalid; see parseManifest.
20 */
21 2 public f<bool> loadManifest(const String& path, const String& label, Manifest& manifest, Vector<String>& errors) {
22 2 Result<String> content = readFile(path.getRaw());
23
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if content.isErr() {
24 1 errors.pushBack(String("Could not read ") + label);
25 1 return false;
26 1 }
27 2 return parseManifest(content.unwrap(), label, manifest, errors);
28 2 }
29
30 /**
31 * Write the text of the manifest to disk. Line breaks are written as they are on every platform.
32 */
33 1 public f<bool> saveManifest(const Manifest& manifest, const String& path = String(MANIFEST_FILE)) {
34 1 Result<File> opened = openFile(path.getRaw(), "wb");
35
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if opened.isErr() { return false; }
36 1 File& file = opened.unwrap();
37 1 bool written = true;
38
4/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 58 times.
✓ Branch 6 taken 58 times.
✓ Branch 7 taken 1 time.
59 for unsigned long i = 0l; i < manifest.text.getLength() && written; i++ { written = file.write(manifest.text[i]); }
39
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 return file.close() && written;
40 ✗ }
41
42 /**
43 * Make the given path absolute by prepending the working dir, if it is relative, and normalize it lexically
44 */
45 51 public f<FilePath> makeAbsolutePath(const FilePath& path) {
46
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 23 times.
51 if path.isAbsolute() {
47 28 return path.lexicallyNormal();
48 }
49 23 const FilePath absolutePath = FilePath(getCurrentWorkingDir()) / path;
50 46 return absolutePath.lexicallyNormal();
51 23 }
52
53 /**
54 * Get the parent dir of the given absolute and lexically normal path. Unlike FilePath.getParentDir, the parent of an entry
55 * directly below the root is the root, e.g. '/' for '/home' or 'C:/' for 'C:/Users'
56 *
57 * @return Parent dir or an empty path, if the given path is the root
58 */
59 221 public f<FilePath> getAbsoluteParentDir(const FilePath& path) {
60 221 FilePath pathCopy = path;
61 221 String parentDir = pathCopy.getParentDir();
62 // The separator of the root is cut off together with the last component
63
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 183 times.
✓ Branch 2 taken 181 times.
✓ Branch 3 taken 40 times.
221 if parentDir.isEmpty() || parentDir.endsWith(":") {
64 40 parentDir += "/";
65 40 }
66 221 const FilePath parentPath = FilePath(parentDir);
67
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 201 times.
221 return parentPath == pathCopy ? FilePath() : parentPath;
68 221 }
69
70 /**
71 * Find the manifest of the project, that the given source file or dir belongs to: the spice.toml file in the dir of the given
72 * path or in the nearest of its parent dirs. The parent dirs are determined lexically, like a shell does it for 'cd ..', so that
73 * a symlinked dir is searched from the location, where it is linked, rather than from its target.
74 *
75 * @param path Path of a source file or dir
76 * @return Absolute path of the manifest or an empty path, if there is none
77 */
78 27 public f<FilePath> findManifest(const FilePath& path) {
79 // Make the path absolute, so that the search can continue above the working dir up to the root
80 27 FilePath dir = makeAbsolutePath(path);
81
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if !dirExists(dir.toString()) {
82 25 dir = getAbsoluteParentDir(dir);
83 25 }
84
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 18 times.
218 while !dir.isEmpty() {
85 200 const FilePath candidate = dir / MANIFEST_FILE;
86
4/4
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 191 times.
200 if fileExists(candidate.toString()) && !dirExists(candidate.toString()) {
87 18 return candidate;
88 }
89 191 dir = getAbsoluteParentDir(dir);
90 191 }
91 36 return FilePath();
92 27 }
93
94 /**
95 * Path of a manifest for messages, relative to the given base dir like the code locations of the compiler: './' for paths
96 * within the base dir, '../' for paths above it and the path as given, if there is no relative one (e.g. on another drive)
97 */
98 11 public f<String> manifestDisplayPath(const FilePath& path, const FilePath& baseDir) {
99 11 const FilePath relativePath = makeAbsolutePath(path).lexicallyRelative(makeAbsolutePath(baseDir));
100 11 const String relativePathStr = String(relativePath.toGenericString());
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if relativePath.isEmpty() {
102 ✗ return String(path.toGenericString());
103 }
104
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
11 if relativePathStr.startsWith("../") {
105 8 return relativePathStr;
106 }
107 14 return "./" + relativePathStr;
108 11 }
109
110 // ============================================================== Tests ==============================================================
111
112 1 #[test]
113 f<bool> testManifestFiles() {
114 1 const String dirName = String("spice-manifest-test-") + toString(getCurrentMicros());
115 1 const FilePath testDir = FilePath(getTempDir()) / dirName;
116 1 const FilePath subDir = testDir / "src" / "sub";
117 1 assert mkDirs(subDir.toString(), MODE_ALL_RWX) == 0;
118 1 const FilePath sourceFile = subDir / "main.spice";
119 1 const Result<bool> sourceWritten = writeFile(sourceFile.toString(), "f<int> main() {}\n");
120 1 assert sourceWritten.isOk();
121
122 // Without manifest, none is found (unless the temp dir is below a project, which it should not be)
123 1 const FilePath noManifest = findManifest(sourceFile);
124
125 // The manifest in a parent dir is found, from a file and from a dir
126 1 const FilePath manifestPath = testDir / MANIFEST_FILE;
127 1 Manifest manifest;
128 1 manifest.name = String("test");
129 1 manifest.version = String("1.0.0");
130 1 manifest.text = renderManifest(manifest);
131 1 const bool saved = saveManifest(manifest, String(manifestPath.toString()));
132 1 const FilePath fromFile = findManifest(sourceFile);
133 1 const FilePath fromDir = findManifest(subDir);
134 1 Manifest loaded;
135 1 Vector<String> errors;
136 1 const bool loadedOk = loadManifest(String(fromFile.toString()), String(MANIFEST_FILE), loaded, errors);
137 1 const String displayFromSubDir = manifestDisplayPath(fromFile, subDir);
138 1 const String displayFromTestDir = manifestDisplayPath(fromFile, testDir);
139 1 Manifest missing;
140 1 const bool missingOk = loadManifest(String((subDir / MANIFEST_FILE).toString()), String("missing"), missing, errors);
141
142 // Clean up before asserting, so that a failure does not leave the files behind
143 1 deleteFile(manifestPath.toString());
144 1 deleteFile(sourceFile.toString());
145 1 rmDir(subDir.toString());
146 1 rmDir((testDir / "src").toString());
147 1 rmDir(testDir.toString());
148
149 1 assert noManifest.isEmpty();
150 1 assert saved && loadedOk;
151 1 assert loaded.name == "test" && loaded.version == "1.0.0";
152 1 assert fromFile.toGenericString() == fromDir.toGenericString();
153 1 assert String(fromFile.toGenericString()).endsWith("/spice.toml");
154 1 assert displayFromSubDir == "../../spice.toml";
155 1 assert displayFromTestDir == "./spice.toml";
156 1 assert !missingOk && errors.getSize() == 1l;
157 1 const String& missingError = errors.get(0l);
158 1 assert missingError == "Could not read missing";
159 1 return true;
160 1 }
161
162 1 #[test]
163 f<bool> testManifestParentDirs() {
164 1 assert getAbsoluteParentDir(FilePath("/home/user")).toGenericString() == "/home";
165 1 assert getAbsoluteParentDir(FilePath("/home")).toGenericString() == "/";
166 1 assert getAbsoluteParentDir(FilePath("/")).isEmpty();
167 1 assert getAbsoluteParentDir(FilePath("C:/Users")).toGenericString() == "C:/";
168 1 assert getAbsoluteParentDir(FilePath("C:/")).isEmpty();
169 // The search starts at the root for a source file in the root
170 1 assert makeAbsolutePath(FilePath("/main.spice")).toGenericString() == "/main.spice";
171 1 assert makeAbsolutePath(FilePath("/")).toGenericString() == "/";
172 1 return true;
173 1 }
174