Skip to content

Commit f8e4b59

Browse files
authored
fix(schematics): fs writeFile create check (#1526)
1 parent a356169 commit f8e4b59

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { EmptyTree } from "@angular-devkit/schematics";
2+
import { UnitTestTree } from "@angular-devkit/schematics/testing";
3+
import { NgTreeFileSystem } from "./NgFileSystem";
4+
5+
describe("NgTreeFileSystem", () => {
6+
let tree: UnitTestTree;
7+
let fs: NgTreeFileSystem;
8+
9+
beforeEach(() => {
10+
tree = new UnitTestTree(new EmptyTree());
11+
fs = new NgTreeFileSystem(tree);
12+
});
13+
14+
describe("fileExists", () => {
15+
it("should return true when the file exists in the tree", () => {
16+
tree.create("/src/app/app.component.ts", "content");
17+
expect(fs.fileExists("/src/app/app.component.ts")).toBeTrue();
18+
});
19+
20+
it("should return false when the file does not exist in the tree", () => {
21+
expect(fs.fileExists("/src/missing.ts")).toBeFalse();
22+
});
23+
});
24+
25+
describe("readFile", () => {
26+
it("should return the file content as a string", () => {
27+
tree.create("/src/app/app.component.ts", "export class AppComponent {}");
28+
expect(fs.readFile("/src/app/app.component.ts")).toBe("export class AppComponent {}");
29+
});
30+
31+
it("should return an empty string when the file does not exist", () => {
32+
expect(fs.readFile("/src/nonexistent.ts")).toBe("");
33+
});
34+
35+
it("should ignore the encoding parameter and still return the content", () => {
36+
tree.create("/src/file.txt", "hello");
37+
expect(fs.readFile("/src/file.txt", "utf-8")).toBe("hello");
38+
});
39+
});
40+
41+
describe("writeFile", () => {
42+
it("should create a new file when it does not exist", () => {
43+
fs.writeFile("/src/new-file.ts", "new content");
44+
expect(tree.readContent("/src/new-file.ts")).toBe("new content");
45+
});
46+
47+
it("should overwrite an existing file", () => {
48+
tree.create("/src/existing.ts", "original content");
49+
fs.writeFile("/src/existing.ts", "updated content");
50+
expect(tree.readContent("/src/existing.ts")).toBe("updated content");
51+
});
52+
});
53+
54+
describe("directoryExists", () => {
55+
it("should return true when the directory contains files", () => {
56+
tree.create("/src/app/app.component.ts", "");
57+
expect(fs.directoryExists("/src/app")).toBeTrue();
58+
});
59+
60+
it("should return true when the directory contains subdirectories", () => {
61+
tree.create("/src/app/nested/file.ts", "");
62+
expect(fs.directoryExists("/src/app")).toBeTrue();
63+
});
64+
65+
it("should return false for an empty or non-existent directory", () => {
66+
expect(fs.directoryExists("/src/nonexistent")).toBeFalse();
67+
});
68+
});
69+
70+
describe("glob", () => {
71+
beforeEach(() => {
72+
tree.create("/src/app/app.component.ts", "");
73+
tree.create("/src/app/app.module.ts", "");
74+
tree.create("/src/app/shared/shared.component.ts", "");
75+
tree.create("/src/environments/environment.ts", "");
76+
tree.create("/src/environments/environment.prod.ts", "");
77+
});
78+
79+
it("should return all files matching the pattern", () => {
80+
const results = fs.glob("/src", "**/*.ts");
81+
expect(results).toContain("/src/app/app.component.ts");
82+
expect(results).toContain("/src/app/app.module.ts");
83+
expect(results).toContain("/src/app/shared/shared.component.ts");
84+
expect(results).toContain("/src/environments/environment.ts");
85+
expect(results).toContain("/src/environments/environment.prod.ts");
86+
});
87+
88+
it("should return only files matching a specific pattern", () => {
89+
const results = fs.glob("/src", "**/environment*.ts");
90+
expect(results).toContain("/src/environments/environment.ts");
91+
expect(results).toContain("/src/environments/environment.prod.ts");
92+
expect(results).not.toContain("/src/app/app.component.ts");
93+
});
94+
95+
it("should return an empty array when no files match the pattern", () => {
96+
const results = fs.glob("/src", "**/*.html");
97+
expect(results).toEqual([]);
98+
});
99+
100+
it("should skip subdirectories matching ignorePatterns", () => {
101+
tree.create("/src/node_modules/lib/index.ts", "");
102+
103+
const results = fs.glob("/src", "**/*.ts", ["node_modules"]);
104+
expect(results.some(r => r.includes("node_modules"))).toBeFalse();
105+
});
106+
107+
it("should return an empty array when dirPath does not exist", () => {
108+
const results = fs.glob("/nonexistent", "**/*.ts");
109+
expect(results).toEqual([]);
110+
});
111+
});
112+
});

packages/ng-schematics/src/utils/NgFileSystem.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export class NgTreeFileSystem implements IFileSystem {
1414
}
1515

1616
public writeFile(filePath: string, text: string): void {
17-
return this.tree.overwrite(filePath, text);
17+
this.tree.exists(filePath)
18+
? this.tree.overwrite(filePath, text)
19+
: this.tree.create(filePath, text);
1820
}
1921

2022
public directoryExists(dirPath: string): boolean {

0 commit comments

Comments
 (0)