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

@ -30,5 +30,8 @@ const debugArtifacts = __importStar(require("./debug-artifacts"));
t.deepEqual(debugArtifacts.sanitizeArifactName("hello===123"), "hello123");
t.deepEqual(debugArtifacts.sanitizeArifactName("*m)a&n^y%i££n+v!a:l[i]d"), "manyinvalid");
});
// TODO(angelapwen): Test uploadDebugArtifacts if toUpload is empty
(0, ava_1.default)("uploadDebugArtifacts", async (t) => {
// Test that no error is thrown if artifacts list is empty.
await t.notThrowsAsync(debugArtifacts.uploadDebugArtifacts([], "rootDir", "artifactName"));
});
//# sourceMappingURL=debug-artifacts.test.js.map

View file

@ -1 +1 @@
{"version":3,"file":"debug-artifacts.test.js","sourceRoot":"","sources":["../src/debug-artifacts.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,8CAAuB;AAEvB,kEAAoD;AAEpD,IAAA,aAAI,EAAC,qBAAqB,EAAE,CAAC,CAAC,EAAE,EAAE;IAChC,CAAC,CAAC,SAAS,CACT,cAAc,CAAC,mBAAmB,CAAC,cAAc,CAAC,EAClD,cAAc,CACf,CAAC;IACF,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,mBAAmB,CAAC,cAAc,CAAC,EAAE,YAAY,CAAC,CAAC;IAC9E,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,mBAAmB,CAAC,aAAa,CAAC,EAAE,UAAU,CAAC,CAAC;IAC3E,CAAC,CAAC,SAAS,CACT,cAAc,CAAC,mBAAmB,CAAC,yBAAyB,CAAC,EAC7D,aAAa,CACd,CAAC;AACJ,CAAC,CAAC,CAAC;AAEH,mEAAmE"}
{"version":3,"file":"debug-artifacts.test.js","sourceRoot":"","sources":["../src/debug-artifacts.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,8CAAuB;AAEvB,kEAAoD;AAEpD,IAAA,aAAI,EAAC,qBAAqB,EAAE,CAAC,CAAC,EAAE,EAAE;IAChC,CAAC,CAAC,SAAS,CACT,cAAc,CAAC,mBAAmB,CAAC,cAAc,CAAC,EAClD,cAAc,CACf,CAAC;IACF,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,mBAAmB,CAAC,cAAc,CAAC,EAAE,YAAY,CAAC,CAAC;IAC9E,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,mBAAmB,CAAC,aAAa,CAAC,EAAE,UAAU,CAAC,CAAC;IAC3E,CAAC,CAAC,SAAS,CACT,cAAc,CAAC,mBAAmB,CAAC,yBAAyB,CAAC,EAC7D,aAAa,CACd,CAAC;AACJ,CAAC,CAAC,CAAC;AAEH,IAAA,aAAI,EAAC,sBAAsB,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IACvC,2DAA2D;IAC3D,MAAM,CAAC,CAAC,cAAc,CACpB,cAAc,CAAC,oBAAoB,CAAC,EAAE,EAAE,SAAS,EAAE,cAAc,CAAC,CACnE,CAAC;AACJ,CAAC,CAAC,CAAC"}

3
lib/util.js generated
View file

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

File diff suppressed because one or more lines are too long

40
lib/util.test.js generated
View file

@ -24,6 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path_1 = __importDefault(require("path"));
const stream = __importStar(require("stream"));
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
@ -315,10 +316,37 @@ for (const [version, githubVersion, shouldReportWarning,] of CHECK_ACTION_VERSIO
isActionsStub.restore();
});
}
// TODO(angelapwen): Test doesDirectoryExist() returns true if directory
// TODO(angelapwen): Test doesDirectoryExist() returns false if file
// TODO(angelapwen): Test doesDirectoryExist() returns false if no file of this type exists
// TODO(angelapwen): Test listFolder() returns files in directory
// TODO(angelapwen): Test listFolder() returns empty if not a directory
// TODO(angelapwen): Test doesDirectoryExist() returns empty if directory is empty
(0, ava_1.default)("doesDirectoryExist", (t) => {
const tmpDir = fs.mkdtempSync(path_1.default.join(os.tmpdir(), "does-dir-exist-"));
const topLevelFile = "top-level-test-file.txt";
fs.writeFileSync(topLevelFile, "");
fs.writeFileSync(`${tmpDir}/nested-test-file.txt`, "");
// Returns true if directory
t.true(util.doesDirectoryExist(tmpDir));
// Returns false if file
t.false(util.doesDirectoryExist(topLevelFile));
// Returns false if no file of this type exists
t.false(util.doesDirectoryExist("non-existent-file.txt"));
// Clean up test files.
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.unlinkSync(topLevelFile);
});
(0, ava_1.default)("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_1.default.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_1.default.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`,
]);
});
//# sourceMappingURL=util.test.js.map

File diff suppressed because one or more lines are too long

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) {