Skip to content

Commit

Permalink
Test/multiple tests (#50)
Browse files Browse the repository at this point in the history
* Ignore generated files in testing

* Add sample test

* Add install test

* Add init test

* Add list test

* Add uninstall test

* Add update test

* Add help test

* Remove redundant double quote from eTag

* Output colored error message when specify duplicate name

* Close subprocess before finish post process

* Fix deno lint violation

* Improve unit test for InstallAction

* Correction of test name

* Stub ky.get and use it in some tests

* Change of test format

* Fix InstallAction test

* Fix ListAction test

* Fix UninstallAction test

* Fix InitAction test

* Fix updateAction test

* Correction of test name

* Delete help test

* Correction of Japanese comments.

* Temporary commit

* Update interactive search test pattern

* Change "ky" to "Stub" and Assert added

* Correction of test name

* Ensure temporary directory and ignore error if does not exist it

* Changes to test summaries

* Renaming files

* Fixing install's assert without specifying the url

* Add test data

* Rewriting of version and pass

* Additional correction of error messages

* Minor correction groups

* Fix SearchAction test

* Remove unnecessary import

* Changes to the install.test overview

* Assert and Stub modifications.

* Changes to the update.test overview

* Tests when downloads fail

* delete only

* Allow blob data to specify stub for ky.get

* check xlsx-to-csv

* Amendments to xlsx and test summaries.

* Corrected location of assert

* Change to copyFileSync.

* Mock apropriate target to suppress duplicate calling for SearchAction#exexute

* Assert only rows parameter when specify number

* Cancellation of constants

* Changing the location of createEmptyDimJson

* Dummy of the date

* Organising the Stub

* Changes to test summaries

* Fix SearchAction test

* Remove denoExitStub

* Fix actual and expected being reversed

* Add unittest for communication error while install in search command

* SUse original Deno.exit method to stop execution while testing

* Add without keyword test

* Add error message to test

* Fix install test

* Fix SearchAction test

* Fix test description

* Remove only

* Remove whitespace and add blank line

* Restore current directory to output coverage report to original place

* Fix xckan_site_url and url

* Support for unzip testing of darwin os.

* Support for unzip test of search on darwin os

Co-authored-by: Sheile <sheile1024@gmail.com>
Co-authored-by: TakayasuKoura <5574536+TakayasuKoura@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 5, 2022
1 parent 1f4247b commit e3d4108
Show file tree
Hide file tree
Showing 16 changed files with 3,987 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
.DS_Store
data_files
dim.json
dim-lock.json
dim-lock.json

# Test results
tests/coverage
tests/temporary
5 changes: 4 additions & 1 deletion libs/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DIM_FILE_VERSION,
DIM_LOCK_FILE_VERSION,
} from "./consts.ts";
import { Colors } from "../deps.ts";

export class DimFileAccessor {
private dimJSON: DimJSON | undefined;
Expand All @@ -13,7 +14,9 @@ export class DimFileAccessor {
Deno.statSync(path);
this.dimJSON = JSON.parse(Deno.readTextFileSync(path));
} catch {
console.log("Not found a dim.json. You should run a 'dim init'. ");
console.log(
Colors.red("Not found a dim.json. You should run a 'dim init'. "),
);
Deno.exit(1);
}
}
Expand Down
16 changes: 11 additions & 5 deletions libs/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ const installFromURL = async (
headers: headers,
};
const responseHeaders = result.response.headers;
lockContent.eTag = responseHeaders.get("etag");
lockContent.eTag = responseHeaders.get("etag")?.replace(/^"(.*)"$/, "$1") ??
null;
if (responseHeaders.has("last-modified")) {
lockContent.lastModified = new Date(responseHeaders.get("last-modified")!);
}
Expand Down Expand Up @@ -156,7 +157,7 @@ const installFromDimFile = async (
catalogUrl: null,
catalogResourceId: null,
lastModified: lastModified,
eTag: headers.get("etag"),
eTag: headers.get("etag")?.replace(/^"(.*)"$/, "$1") ?? null,
lastDownloaded: new Date(),
integrity: "",
postProcesses: content.postProcesses,
Expand Down Expand Up @@ -298,9 +299,11 @@ export class InstallAction {
}
const targetContent = new DimFileAccessor().getContents().find((c) => c.name === options.name);
if (targetContent !== undefined && !options.force) {
console.log("The name already exists.");
console.log(Colors.red("The name already exists."));
console.log(
"Use the -F option to force installation and overwrite existing files.",
Colors.red(
"Use the -F option to force installation and overwrite existing files.",
),
);
Deno.exit(1);
}
Expand All @@ -326,7 +329,10 @@ export class InstallAction {
options.file || DEFAULT_DIM_FILE_PATH,
options.asyncInstall,
options.force,
);
).catch(() => {
console.log(Colors.red("Selecting other than json."));
Deno.exit(1);
});
if (lockContentList !== undefined) {
if (lockContentList.length != 0) {
console.log(
Expand Down
1 change: 1 addition & 0 deletions libs/postprocess/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class Command extends BasePostprocess {
stdout: "piped",
});
const o = await p.output();
p.close();
return new TextDecoder().decode(o);
}
validate(argumentList: string[]) {
Expand Down
57 changes: 57 additions & 0 deletions tests/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Stub, stub } from "https://deno.land/std@0.152.0/testing/mock.ts";
import { resolve } from "https://deno.land/std@0.152.0/path/mod.ts";
import { ky } from "../deps.ts";

const currentDirectory = new URL(".", import.meta.url).pathname;
export const temporaryDirectory = resolve(currentDirectory, "temporary") + "/";
Deno.mkdirSync(temporaryDirectory, { recursive: true });

export const createKyGetStub = (
body: BodyInit,
options?: ResponseInit,
): Stub => {
const mockedKy = ky.extend({
hooks: {
beforeRequest: [
(_request) => {
return new Response(body, options);
},
],
},
});

return stub(ky, "get", mockedKy.get);
};

export const removeTemporaryFiles = () => {
// Skip removing process when temporary directory does not exist
try {
Deno.statSync(temporaryDirectory);
} catch {
return;
}

for (const path of Deno.readDirSync(temporaryDirectory)) {
Deno.removeSync(temporaryDirectory + path.name, { recursive: true });
}
};

export function fileExists(filePath: string): boolean {
try {
Deno.statSync(filePath);
return true;
} catch {
return false;
}
}

export const createEmptyDimJson = () => {
Deno.writeTextFileSync(
"dim.json",
JSON.stringify({ fileVersion: "1.1", contents: [] }),
);
Deno.writeTextFileSync(
"dim-lock.json",
JSON.stringify({ lockfileVersion: "1.1", contents: [] }),
);
};
45 changes: 45 additions & 0 deletions tests/libs/actions.init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { assertEquals } from "https://deno.land/std@0.152.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/std@0.152.0/testing/mock.ts";
import {
afterEach,
beforeEach,
describe,
it,
} from "https://deno.land/std@0.152.0/testing/bdd.ts";

import { InitAction } from "../../libs/actions.ts";
import { removeTemporaryFiles, temporaryDirectory } from "../helper.ts";

describe("InitAction", () => {
let consoleLogStub: Stub;
let originalDirectory: string;

beforeEach(() => {
consoleLogStub = stub(console, "log");
originalDirectory = Deno.cwd();
Deno.chdir(temporaryDirectory);
});

afterEach(() => {
removeTemporaryFiles();
consoleLogStub.restore();
Deno.chdir(originalDirectory);
});

it("ensure that empty data directories, dim.json and dim-lock.json are created.", async () => {
await new InitAction().execute();

const dataDirectory = Deno.statSync("data_files");
assertEquals(dataDirectory.isDirectory, true);

const dimJson = JSON.parse(
Deno.readTextFileSync("dim.json"),
);
assertEquals(dimJson, { fileVersion: "1.1", contents: [] });

const dimLockJson = JSON.parse(
Deno.readTextFileSync("dim-lock.json"),
);
assertEquals(dimLockJson, { lockFileVersion: "1.1", contents: [] });
});
});
Loading

0 comments on commit e3d4108

Please sign in to comment.