Skip to content

Commit

Permalink
refactor: fix null and undefined warnings in test
Browse files Browse the repository at this point in the history
Assert not-null or not-undefined where applicable
  • Loading branch information
ComradeVanti committed Oct 25, 2023
1 parent a94c468 commit edb8b51
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
16 changes: 16 additions & 0 deletions test/test-cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
removeWorkDir,
} from "./utils";
import testConsole from "test-console";
import assert from "assert";

describe("cmd-add.ts", function () {
const options = {
Expand Down Expand Up @@ -291,6 +292,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a", options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestA);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -300,6 +302,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a@1.0.0", options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestA);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -309,6 +312,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a@latest", options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestA);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -320,6 +324,7 @@ describe("cmd-add.ts", function () {
const retCode2 = await add("com.base.package-a@1.0.0", options);
retCode2.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestA);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("modified ").should.be.ok();
Expand All @@ -331,6 +336,7 @@ describe("cmd-add.ts", function () {
const retCode2 = await add("com.base.package-a@1.0.0", options);
retCode2.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestA);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("existed ").should.be.ok();
Expand All @@ -340,6 +346,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a@2.0.0", options);
retCode.should.equal(1);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(defaultManifest);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("version 2.0.0 is not a valid choice").should.be.ok();
Expand All @@ -350,6 +357,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a@" + gitUrl, options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.dependencies["com.base.package-a"].should.be.equal(gitUrl);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -360,6 +368,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a@" + gitUrl, options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.dependencies["com.base.package-a"].should.be.equal(gitUrl);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -370,6 +379,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a@" + fileUrl, options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.dependencies["com.base.package-a"].should.be.equal(fileUrl);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -379,6 +389,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("pkg-not-exist", options);
retCode.should.equal(1);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.deepEqual(defaultManifest);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("package not found").should.be.ok();
Expand All @@ -390,6 +401,7 @@ describe("cmd-add.ts", function () {
);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestAB);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added com.base.package-a").should.be.ok();
Expand All @@ -400,6 +412,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.upstream.package-up", upstreamOptions);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestUpstream);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added com.upstream.package-up").should.be.ok();
Expand All @@ -409,6 +422,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("pkg-not-exist", upstreamOptions);
retCode.should.equal(1);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.deepEqual(defaultManifest);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("package not found").should.be.ok();
Expand All @@ -417,6 +431,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-c@latest", upstreamOptions);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestC);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand All @@ -426,6 +441,7 @@ describe("cmd-add.ts", function () {
const retCode = await add("com.base.package-a", testableOptions);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(expectedManifestTestable);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("added").should.be.ok();
Expand Down
7 changes: 7 additions & 0 deletions test/test-cmd-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ describe("cmd-remove.ts", function () {
const retCode = await remove("com.example.package-a", options);
retCode.should.equal(0);
const manifest = loadManifest();
assert(manifest !== null);
(
manifest.dependencies["com.example.package-a"] == undefined
).should.be.ok();
assert(manifest.scopedRegistries !== undefined);
assert(manifest.scopedRegistries[0] !== undefined);
manifest.scopedRegistries[0].scopes.should.be.deepEqual([
"com.example",
"com.example.package-b",
Expand All @@ -78,6 +81,7 @@ describe("cmd-remove.ts", function () {
const retCode = await remove("com.example.package-a@1.0.0", options);
retCode.should.equal(1);
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual(defaultManifest);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("please replace").should.be.ok();
Expand Down Expand Up @@ -110,12 +114,15 @@ describe("cmd-remove.ts", function () {
);
retCode.should.equal(0);
const manifest = await loadManifest();
assert(manifest !== null);
(
manifest.dependencies["com.example.package-a"] == undefined
).should.be.ok();
(
manifest.dependencies["com.example.package-b"] == undefined
).should.be.ok();
assert(manifest.scopedRegistries !== undefined);
assert(manifest.scopedRegistries[0] !== undefined);
manifest.scopedRegistries[0].scopes.should.be.deepEqual(["com.example"]);
const [stdout] = getOutputs(stdoutInspect, stderrInspect);
stdout.includes("removed com.example.package-a").should.be.ok();
Expand Down
36 changes: 27 additions & 9 deletions test/test-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
removeWorkDir,
} from "./utils";
import testConsole from "test-console";
import assert from "assert";

describe("cmd-core.ts", function () {
describe("parseEnv", function () {
Expand Down Expand Up @@ -178,6 +179,7 @@ describe("cmd-core.ts", function () {
{ checkPath: true }
)
).should.be.ok();
assert(env.editorVersion !== null);
env.editorVersion.should.be.equal("2019.2.13f1");
});
it("region cn", async function () {
Expand Down Expand Up @@ -233,6 +235,7 @@ describe("cmd-core.ts", function () {
)
).should.be.ok();
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual({ dependencies: {} });
});
it("no manifest file", async function () {
Expand Down Expand Up @@ -267,10 +270,12 @@ describe("cmd-core.ts", function () {
)
).should.be.ok();
const manifest = loadManifest();
assert(manifest !== null);
manifest.should.be.deepEqual({ dependencies: {} });
manifest.dependencies["some-pack"] = "1.0.0";
saveManifest(manifest).should.be.ok();
const manifest2 = loadManifest();
assert(manifest2 !== null);
manifest2.should.be.deepEqual(manifest);
});
});
Expand All @@ -294,6 +299,7 @@ describe("cmd-core.ts", function () {
.get("/package-a")
.reply(200, pkgInfoRemote, { "Content-Type": "application/json" });
const info = await fetchPackageInfo("package-a");
assert(info !== undefined);
info.should.deepEqual(pkgInfoRemote);
});
it("404", async function () {
Expand All @@ -312,9 +318,9 @@ describe("cmd-core.ts", function () {

describe("getLatestVersion", function () {
it("from dist-tags", async function () {
getLatestVersion({ "dist-tags": { latest: "1.0.0" } }).should.equal(
"1.0.0"
);
const version = getLatestVersion({ "dist-tags": { latest: "1.0.0" } });
assert(version !== undefined);
version.should.equal("1.0.0");
});
it("not found", async function () {
(
Expand All @@ -328,17 +334,23 @@ describe("cmd-core.ts", function () {
(parseEditorVersion(null) === null).should.be.ok();
});
it("test x.y", function () {
parseEditorVersion("2019.2").should.deepEqual({ major: 2019, minor: 2 });
const version = parseEditorVersion("2019.2");
assert(version !== null);
version.should.deepEqual({ major: 2019, minor: 2 });
});
it("test x.y.z", function () {
parseEditorVersion("2019.2.1").should.deepEqual({
const version = parseEditorVersion("2019.2.1");
assert(version !== null);
version.should.deepEqual({
major: 2019,
minor: 2,
patch: 1,
});
});
it("test x.y.zan", function () {
parseEditorVersion("2019.2.1a5").should.deepEqual({
const version = parseEditorVersion("2019.2.1a5");
assert(version !== null);
version.should.deepEqual({
major: 2019,
minor: 2,
patch: 1,
Expand All @@ -348,7 +360,9 @@ describe("cmd-core.ts", function () {
});
});
it("test x.y.zbn", function () {
parseEditorVersion("2019.2.1b5").should.deepEqual({
const version = parseEditorVersion("2019.2.1b5");
assert(version !== null);
version.should.deepEqual({
major: 2019,
minor: 2,
patch: 1,
Expand All @@ -358,7 +372,9 @@ describe("cmd-core.ts", function () {
});
});
it("test x.y.zfn", function () {
parseEditorVersion("2019.2.1f5").should.deepEqual({
const version = parseEditorVersion("2019.2.1f5");
assert(version !== null);
version.should.deepEqual({
major: 2019,
minor: 2,
patch: 1,
Expand All @@ -368,7 +384,9 @@ describe("cmd-core.ts", function () {
});
});
it("test x.y.zcn", function () {
parseEditorVersion("2019.2.1f1c5").should.deepEqual({
const version = parseEditorVersion("2019.2.1f1c5");
assert(version !== null);
version.should.deepEqual({
major: 2019,
minor: 2,
patch: 1,
Expand Down

0 comments on commit edb8b51

Please sign in to comment.