Add utilities unit tests

This commit is contained in:
Angela P Wen 2022-08-10 14:57:57 +02:00
parent ff7a29dd72
commit 484a72c924
9 changed files with 92 additions and 17 deletions

View file

@ -15,4 +15,9 @@ test("sanitizeArifactName", (t) => {
);
});
// TODO(angelapwen): Test uploadDebugArtifacts if toUpload is empty
test("uploadDebugArtifacts", async (t) => {
// Test that no error is thrown if artifacts list is empty.
await t.notThrowsAsync(
debugArtifacts.uploadDebugArtifacts([], "rootDir", "artifactName")
);
});

View file

@ -1,5 +1,6 @@
import * as fs from "fs";
import * as os from "os";
import path from "path";
import * as stream from "stream";
import * as core from "@actions/core";
@ -440,14 +441,46 @@ for (const [
});
}
// TODO(angelapwen): Test doesDirectoryExist() returns true if directory
test("doesDirectoryExist", (t) => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "does-dir-exist-"));
// TODO(angelapwen): Test doesDirectoryExist() returns false if file
const topLevelFile = "top-level-test-file.txt";
fs.writeFileSync(topLevelFile, "");
fs.writeFileSync(`${tmpDir}/nested-test-file.txt`, "");
// TODO(angelapwen): Test doesDirectoryExist() returns false if no file of this type exists
// Returns true if directory
t.true(util.doesDirectoryExist(tmpDir));
// TODO(angelapwen): Test listFolder() returns files in directory
// Returns false if file
t.false(util.doesDirectoryExist(topLevelFile));
// TODO(angelapwen): Test listFolder() returns empty if not a directory
// Returns false if no file of this type exists
t.false(util.doesDirectoryExist("non-existent-file.txt"));
// TODO(angelapwen): Test doesDirectoryExist() returns empty if directory is empty
// Clean up test files.
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.unlinkSync(topLevelFile);
});
test("listFolder", (t) => {
// Returns empty if not a directory
t.deepEqual(util.listFolder("not-a-directory"), []);
// Returns empty if directory is empty
const emptyTmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), "list-folder-empty-")
);
t.deepEqual(util.listFolder(emptyTmpDir), []);
fs.rmSync(emptyTmpDir, { recursive: true, force: true });
// Returns all file names in directory
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "list-folder-"));
fs.writeFileSync(`${tmpDir}/test-file-1.txt`, "");
fs.writeFileSync(`${tmpDir}/test-file-2.txt`, "");
fs.writeFileSync(`${tmpDir}/test-file-3.txt`, "");
t.deepEqual(util.listFolder(tmpDir), [
`${tmpDir}/test-file-1.txt`,
`${tmpDir}/test-file-2.txt`,
`${tmpDir}/test-file-3.txt`,
]);
});

View file

@ -773,6 +773,9 @@ export function doesDirectoryExist(dirPath: string): boolean {
* Returns a list of files in a given directory.
*/
export function listFolder(dir: string): string[] {
if (!doesDirectoryExist(dir)) {
return [];
}
const entries = fs.readdirSync(dir, { withFileTypes: true });
let files: string[] = [];
for (const entry of entries) {