Skip to content

Commit

Permalink
♻️ env Add stub tests; see #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Valen committed Sep 25, 2022
1 parent 705e64a commit 30f2b8c
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions __mocks__/valid.nixt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> { }, nixt }:

nixt.mkSuites {
"Valid Tests" = {
"always passes" = true;
};
}
45 changes: 45 additions & 0 deletions src/App.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import "reflect-metadata";

import { Container } from "inversify";
import { IApp, IArgParser } from "./interfaces.js";
import { bindings } from "./bindings.js";

describe("App", () => {
let container: Container;
let sut: IApp;

beforeAll(() => {
const mockArgParser: IArgParser = {
run: () => {
const args = {
paths: ["."],
watch: false,
verbose: [false, false],
list: false,
recurse: false,
debug: false,
help: false
};

return args;
}
};

container = new Container;
container.loadAsync(bindings);
container.unbind(IArgParser);
container.bind(IArgParser).toConstantValue(mockArgParser)
sut = container.get(IApp);
})

beforeEach(() => {
container.snapshot();
})

it("is defined", () => {
expect(sut).toBeDefined();
})

it.todo("returns non-zero exit code on fail")
it.todo("returns non-zero exit code on import err")
})
27 changes: 27 additions & 0 deletions src/components/cli/ArgParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "reflect-metadata";

import { Container } from "inversify";
import { bindings } from "../../bindings.js";
import { IArgParser } from "../../interfaces.js";

describe("ArgParser", () => {
let container: Container;
let sut: IArgParser;

beforeAll(() => {
container = new Container;
container.loadAsync(bindings);
sut = container.get(IArgParser);
})

beforeEach(() => {
container.snapshot();
})

it("is defined", () => {
expect(sut).toBeDefined();
})

it.todo("returns sensible defaults");
it.todo("returns correct structure");
})
24 changes: 24 additions & 0 deletions src/components/cli/RenderService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import "reflect-metadata";

import { Container } from "inversify";
import { bindings } from "../../bindings.js";
import { IRenderService } from "../../interfaces.js";

describe("RenderService", () => {
let container: Container;
let sut: IRenderService;

beforeAll(() => {
container = new Container;
container.loadAsync(bindings);
sut = container.get(IRenderService);
})

beforeEach(() => {
container.snapshot();
})

it("is defined", () => {
expect(sut).toBeDefined();
})
})
22 changes: 20 additions & 2 deletions src/components/nix/NixService.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "reflect-metadata";

import { Container } from "inversify";
import { INixService } from "../../interfaces.js";
import { resolve } from "node:path";
import { bindings } from "../../bindings.js";
import { INixService } from "../../interfaces.js";

describe("NixService", () => {
let container: Container;
Expand All @@ -12,15 +13,32 @@ describe("NixService", () => {
container = new Container;
container.loadAsync(bindings);
sut = container.get(INixService);
})

beforeEach(() => {
container.snapshot();
})

afterEach(() => {
container.restore();
})

test("is defined", () => {
it("is defined", () => {
expect(sut).toBeDefined();
})

it("returns nix results", () => {
const p = resolve("__mocks__/valid.nixt");
const expected = { "path": p, "suites": { "Valid Tests": ["always passes"] } }

const result = sut.eval("get-testspec.nix", {
trace: false,
debug: false,
args: { path: p }
})

expect(result).toStrictEqual(expected)
})

it.todo("throws error on invalid path")
})
9 changes: 4 additions & 5 deletions src/components/nix/NixService.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import path from "path";
import { execSync } from "child_process";

import { injectable } from "inversify";
import { INixService } from "../../interfaces.js";
import { NixOptions, Path } from "../../types.js";
import { execSync } from "node:child_process";
import { join } from "node:path";

@injectable()
export class NixService implements INixService {
public eval(file: Path, options: NixOptions) {
public eval(file: Path, options: NixOptions): any {
const nixPath = (f: Path) => {
return path.join(import.meta.url.replace("file:", ""), "..", "..", "..", "..", "nix", f);
return join(import.meta.url.replace("file:", ""), "..", "..", "..", "nix", f);
}

const generateCallArgs = (a: {}) => {
Expand Down
27 changes: 27 additions & 0 deletions src/components/test/TestFinder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "reflect-metadata";

import { Container } from "inversify";
import { bindings } from "../../bindings.js";
import { ITestFinder } from "../../interfaces.js";

describe("ItFinder", () => {
let container: Container;
let sut: ITestFinder;

beforeAll(() => {
container = new Container;
container.loadAsync(bindings);
sut = container.get(ITestFinder);
})

beforeEach(() => {
container.snapshot();
})

it("is defined", () => {
expect(sut).toBeDefined();
})

it.todo("returns correct structure");
it.todo("sets importError values");
})
6 changes: 3 additions & 3 deletions src/components/test/TestFinder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import { resolve } from 'path';

import { inject, injectable } from "inversify";
// TODO use fs/promises
import fs from 'node:fs';
import { resolve } from 'node:path';
import { INixService, ITestFinder } from "../../interfaces.js";
import { CliArgs, Path, TestCase, TestFile, TestSpec, TestSuite } from "../../types.js";

Expand Down
26 changes: 26 additions & 0 deletions src/components/test/TestRunner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "reflect-metadata";

import { Container } from "inversify";
import { bindings } from "../../bindings.js";
import { ITestRunner } from "../../interfaces.js";

describe("TestRunner", () => {
let container: Container;
let sut: ITestRunner;

beforeAll(() => {
container = new Container;
container.loadAsync(bindings);
sut = container.get(ITestRunner);
})

beforeEach(() => {
container.snapshot();
})

it("is defined", () => {
expect(sut).toBeDefined();
})

it.todo("handles failed tests");
})
38 changes: 19 additions & 19 deletions src/components/test/TestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { resolve } from "path";

import { inject, injectable } from "inversify";
import { resolve } from "node:path";
import { INixService, ITestRunner } from "../../interfaces.js";
import { CliArgs, NixOptions, Path, TestCase, TestFile, TestSuite } from "../../types.js";

Expand All @@ -14,23 +13,24 @@ export class TestRunner implements ITestRunner {
this._nixService = nixService;
}

public async run(args: CliArgs, files: TestFile[]): Promise<void> {
const getTestCase = (
f: Path,
s: TestSuite["name"],
c: TestCase["name"],
t: NixOptions["trace"]
) => {
return this._nixService.eval("get-testcase.nix", {
trace: t,
args: {
path: resolve(f),
suite: s,
case: c
}
})
}
private getTestCase(
f: Path,
s: TestSuite["name"],
c: TestCase["name"],
t: NixOptions["trace"]
) {
return this._nixService.eval("get-testcase.nix", {
trace: t,
args: {
path: resolve(f),
suite: s,
case: c
}
})
}

// TODO return TestFile[] to improve testability
public async run(args: CliArgs, files: TestFile[]): Promise<void> {
let traceArg = false;
if (args.verbose[1]) traceArg = true;

Expand All @@ -40,7 +40,7 @@ export class TestRunner implements ITestRunner {
for (const [cName, c] of Object.entries(s.cases)) {
if (args.debug) console.log(cName);
try {
c.result = getTestCase(f.path, sName, cName, traceArg);
c.result = this.getTestCase(f.path, sName, cName, traceArg);
} catch (e: any) {
c.error = e.message;
}
Expand Down

0 comments on commit 30f2b8c

Please sign in to comment.