-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcmake-projects.test.mts
More file actions
150 lines (127 loc) · 5.06 KB
/
cmake-projects.test.mts
File metadata and controls
150 lines (127 loc) · 5.06 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
import assert from "node:assert/strict";
import { describe, it, TestContext } from "node:test";
import path from "node:path";
import fs from "node:fs";
import os from "node:os";
import { EXAMPLES_DIR, findCMakeProjects } from "./cmake-projects.mjs";
function setupTempDirectory(context: TestContext, files: Record<string, string>) {
const tempDirectoryPath = fs.realpathSync(
fs.mkdtempSync(path.join(os.tmpdir(), "cmake-projects-test-"))
);
context.after(() => {
fs.rmSync(tempDirectoryPath, { recursive: true, force: true });
});
for (const [filePath, content] of Object.entries(files)) {
const fullPath = path.join(tempDirectoryPath, filePath);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, content, "utf8");
}
return tempDirectoryPath;
}
describe("EXAMPLES_DIR", () => {
it("should resolve to a valid platform-specific path", () => {
// Check that EXAMPLES_DIR is an absolute path
assert(path.isAbsolute(EXAMPLES_DIR), "EXAMPLES_DIR should be absolute");
// On Windows, should not start with /
if (process.platform === "win32") {
assert(
!EXAMPLES_DIR.startsWith("/"),
"Windows path should not start with /"
);
// Should match Windows path pattern (e.g., C:\... or D:\...)
assert(
/^[A-Za-z]:[\\/]/.test(EXAMPLES_DIR),
"Windows path should start with drive letter"
);
} else {
// On Unix-like systems, should start with /
assert(
EXAMPLES_DIR.startsWith("/"),
"Unix path should start with /"
);
}
});
it("should work correctly with path.join operations", (context) => {
const tempDir = setupTempDirectory(context, {
"test/subdir/file.txt": "test content",
});
// Simulate what happens in copy-examples.mts
const relativePath = "test/subdir";
const joinedPath = path.join(tempDir, relativePath);
// The joined path should exist and be accessible
assert(fs.existsSync(joinedPath), "Joined path should exist");
assert(
fs.statSync(joinedPath).isDirectory(),
"Joined path should be a directory"
);
});
it("should handle URL to path conversion correctly on all platforms", () => {
// Create a test URL similar to how EXAMPLES_DIR is created
const testUrl = new URL("../examples", import.meta.url);
const convertedPath = testUrl.pathname;
// The converted path should work with fs operations
// We can't test the actual EXAMPLES_DIR since it might not exist,
// but we can verify the conversion produces valid paths
if (process.platform === "win32") {
// On Windows, URL.pathname returns /C:/... which is invalid
// Our fix uses fileURLToPath which returns C:\...
assert(
!path.isAbsolute(convertedPath) || convertedPath.startsWith("/"),
"Direct URL.pathname on Windows produces invalid absolute paths"
);
}
});
});
describe("findCMakeProjects", () => {
it("should find CMakeLists.txt files recursively", (context) => {
const tempDir = setupTempDirectory(context, {
"project1/CMakeLists.txt": "# CMake file 1",
"project2/subdir/CMakeLists.txt": "# CMake file 2",
"project3/CMakeLists.txt": "# CMake file 3",
"not-a-project/other.txt": "not cmake",
});
const projects = findCMakeProjects(tempDir);
assert.equal(projects.length, 3, "Should find 3 CMake projects");
// Sort for consistent comparison
const sortedProjects = projects.sort();
const expectedProjects = [
path.join(tempDir, "project1"),
path.join(tempDir, "project2", "subdir"),
path.join(tempDir, "project3"),
].sort();
assert.deepEqual(sortedProjects, expectedProjects);
});
it("should handle empty directories", (context) => {
const tempDir = setupTempDirectory(context, {});
const projects = findCMakeProjects(tempDir);
assert.equal(projects.length, 0, "Should find no projects in empty dir");
});
it("should handle nested CMake projects", (context) => {
const tempDir = setupTempDirectory(context, {
"parent/CMakeLists.txt": "# Parent CMake",
"parent/child/CMakeLists.txt": "# Child CMake",
"parent/child/grandchild/CMakeLists.txt": "# Grandchild CMake",
});
const projects = findCMakeProjects(tempDir);
assert.equal(projects.length, 3, "Should find all nested projects");
assert(
projects.includes(path.join(tempDir, "parent")),
"Should include parent project"
);
assert(
projects.includes(path.join(tempDir, "parent", "child")),
"Should include child project"
);
assert(
projects.includes(path.join(tempDir, "parent", "child", "grandchild")),
"Should include grandchild project"
);
});
it("should work with Windows-style paths", { skip: process.platform !== "win32" }, (context) => {
const tempDir = setupTempDirectory(context, {
"windows\\style\\path\\CMakeLists.txt": "# CMake file",
});
const projects = findCMakeProjects(tempDir);
assert.equal(projects.length, 1, "Should find project with Windows path");
});
});