Skip to content

Commit

Permalink
[TASK] Add more tests for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
s2b committed May 12, 2024
1 parent 67059fa commit 6ee724b
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 16 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/mock-fs": "^4.13.4",
"@types/node": "^20.8.2",
"jest": "^29.7.0",
"mock-fs": "^5.2.0",
"prettier": "^3.0.3",
"ts-jest": "^29.1.2",
"typescript": "^5.2.2",
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function initializePluginConfig(
}

export function readJsonFile(file: string): any {
return JSON.parse(readFileSync(file, "utf-8")) || {};
return JSON.parse(readFileSync(file, "utf-8"));
}

export function isComposerRoot(file: string): boolean {
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions test/fixtures/composerProject/vendor/composer/installed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"packages": [

]
}
File renamed without changes.
Empty file.
79 changes: 64 additions & 15 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
import { describe, expect, test } from "@jest/globals";
import { isComposerRoot } from "../src/utils";
import { describe, expect, test, beforeAll, afterAll } from "@jest/globals";
import { determineComposerRoot, isComposerRoot, readJsonFile } from "../src/utils";
import { resolve } from "node:path";
import mockFs from "mock-fs";

describe("utils module", () => {
test("checks projectComposerFile.json to be valid root composer file", () => {
beforeAll(() => {
mockFs({
"/path/to/fixtures": mockFs.load(resolve(__dirname, "fixtures"))
});
})
afterAll(() => {
mockFs.restore();
})

describe("isComposerRoot", () => {
test("checks project composer.json to be valid root composer file", () => {
expect(
isComposerRoot(
resolve(__dirname, "fixtures/projectComposerFile.json"),
),
isComposerRoot("/path/to/fixtures/composerProject/composer.json")
).toBe(true);
});
test("checks extensionComposerFile.json to be invalid root composer file", () => {
test("checks extension composer.json to be invalid root composer file", () => {
expect(
isComposerRoot(
resolve(__dirname, "fixtures/extensionComposerFile.json"),
),
isComposerRoot("/path/to/fixtures/composerProject/packages/composerExtension/composer.json")
).toBe(false);
});
test("checks libraryComposerFile.json to be invalid root composer file", () => {
test("checks library composer.json to be invalid root composer file", () => {
expect(
isComposerRoot(
resolve(__dirname, "fixtures/libraryComposerFile.json"),
),
isComposerRoot("/path/to/fixtures/composerProject/vendor/namespace/library/composer.json")
).toBe(false);
});
});

describe("readJsonFile", () => {
test("reads composer.json as json", () => {
expect(
readJsonFile("/path/to/fixtures/composerProject/vendor/namespace/library/composer.json")
).toEqual({"type": "library"});
});
});

describe("determineComposerRoot", () => {
test("determines composer root from extension path", () => {
expect(
determineComposerRoot("/path/to/fixtures/composerProject/packages/composerExtension")
).toBe("/path/to/fixtures/composerProject");
});
test("determines composer root from library path", () => {
expect(
determineComposerRoot("/path/to/fixtures/composerProject/vendor/namespace/library")
).toBe("/path/to/fixtures/composerProject");
});
test("determines composer root from root path", () => {
expect(
determineComposerRoot("/path/to/fixtures/composerProject")
).toBe("/path/to/fixtures/composerProject");
});
test("determines composer root from other path", () => {
expect(
determineComposerRoot("/path/to/fixtures/composerProject/packages")
).toBe("/path/to/fixtures/composerProject");
});

test("fail to determine composer root from extension path", () => {
expect(
determineComposerRoot("/path/to/fixtures/nonComposerProject/typo3conf/ext/non_composer_extension")
).toBe("/path/to/fixtures/nonComposerProject/typo3conf/ext/non_composer_extension");
});
test("fail to determine composer root from extension path with fallback root specified", () => {
expect(
determineComposerRoot(
"/path/to/fixtures/nonComposerProject/typo3conf/ext/non_composer_extension",
"/path/to/fixtures/nonComposerProject",
)
).toBe("/path/to/fixtures/nonComposerProject");
});
});

0 comments on commit 6ee724b

Please sign in to comment.