-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sönke Hahn <[email protected]>
- Loading branch information
1 parent
09db1b6
commit f075a8a
Showing
13 changed files
with
307 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { Check } from "./check.ts"; | ||
import { Executable } from "./executable.ts"; | ||
import { Project } from "./project.ts"; | ||
import { Plugin, Project } from "./project.ts"; | ||
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts"; | ||
import * as garn from "./mod.ts"; | ||
import * as nix from "./nix.ts"; | ||
import { assertStdout, assertSuccess, runExecutable } from "./testUtils.ts"; | ||
import { Package } from "./mod.ts"; | ||
|
||
const assertTypeIsCheck = (_c: Check) => {}; | ||
const assertTypeIsExecutable = (_e: Executable) => {}; | ||
const assertTypeIsPackage = (_p: Package) => {}; | ||
|
||
const _testTypeCheckingOfAddCheck = (project: Project) => { | ||
const p = project | ||
|
@@ -50,3 +56,151 @@ const _testTypeCheckingOfAddExecutableTemplate = (project: Project) => { | |
// @ts-expect-error - shell should be the actual executable now, not the helper | ||
p.shell``; | ||
}; | ||
|
||
describe("Project.add", () => { | ||
it("allows adding fields with .add", () => { | ||
const project = garn | ||
.mkProject({ description: "" }, {}) | ||
.add((self) => ({ ...self, foo: garn.shell("echo foo") })); | ||
const output = runExecutable(project.foo); | ||
assertSuccess(output); | ||
assertStdout(output, "foo\n"); | ||
}); | ||
|
||
it("allows adding fields while referencing the Project", () => { | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{}, | ||
) | ||
.withDevTools([garn.mkPackage(nix.nixRaw`pkgs.hello`, "")]) | ||
.add((self) => ({ ...self, foo: self.shell("hello") })); | ||
const output = runExecutable(project.foo); | ||
assertSuccess(output); | ||
assertStdout(output, "Hello, world!\n"); | ||
}); | ||
|
||
it("allows splicing in existing fields", () => { | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{ | ||
package: garn.build` | ||
mkdir -p $out/bin | ||
echo 'echo main executable' > $out/bin/main | ||
chmod +x $out/bin/main | ||
`, | ||
}, | ||
) | ||
.add((self) => ({ ...self, foo: self.shell`${self.package}/bin/main` })); | ||
const output = runExecutable(project.foo); | ||
assertSuccess(output); | ||
assertStdout(output, "main executable\n"); | ||
}); | ||
|
||
it("allows bundling multiple changes into plugins", () => { | ||
const plugin = <P extends garn.Project>(p: P) => | ||
p.addExecutable("foo", "echo foo").addExecutable("bar", "echo bar"); | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{}, | ||
) | ||
.add(plugin); | ||
let output = runExecutable(project.foo); | ||
assertSuccess(output); | ||
assertStdout(output, "foo\n"); | ||
output = runExecutable(project.bar); | ||
assertSuccess(output); | ||
assertStdout(output, "bar\n"); | ||
}); | ||
|
||
it("allows writing plugins with parameters", () => { | ||
const plugin = | ||
<P extends garn.Project>(config: { a: string; b: string }) => | ||
(p: P) => | ||
p | ||
.addExecutable("a", `echo ${config.a}`) | ||
.addExecutable("b", `echo ${config.b}`); | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{}, | ||
) | ||
.add(plugin({ a: "foo", b: "bar" })); | ||
let output = runExecutable(project.a); | ||
assertSuccess(output); | ||
assertStdout(output, "foo\n"); | ||
output = runExecutable(project.b); | ||
assertSuccess(output); | ||
assertStdout(output, "bar\n"); | ||
}); | ||
|
||
it("provides a nice type synonym for plugins that add a field", () => { | ||
const plugin: Plugin<{ addedField: garn.Package }> = (p) => ({ | ||
addedField: garn.build``, | ||
}); | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{}, | ||
) | ||
.addExecutable("foo", "") | ||
.add(plugin); | ||
assertTypeIsExecutable(project.foo); | ||
assertTypeIsPackage(project.addedField); | ||
}); | ||
|
||
it("provides a nice type synonym for plugins that add multiple fields", () => { | ||
const plugin: Plugin<{ one: garn.Package; two: garn.Check }> = (p) => ({ | ||
one: garn.build``, | ||
two: garn.check(""), | ||
}); | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{}, | ||
) | ||
.addExecutable("foo", "") | ||
.add(plugin); | ||
assertTypeIsExecutable(project.foo); | ||
assertTypeIsPackage(project.one); | ||
assertTypeIsCheck(project.two); | ||
}); | ||
|
||
it("provides a nice interface for plugins that depend on a non-standard field", () => { | ||
const plugin: Plugin<{ addedField: Executable }, { dep: Package }> = ( | ||
p, | ||
) => ({ | ||
addedField: garn.shell`${p.dep}/bin/whatever`, | ||
}); | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{ dep: garn.build`` }, | ||
) | ||
.addExecutable("foo", "") | ||
.add(plugin); | ||
assertTypeIsExecutable(project.foo); | ||
assertTypeIsExecutable(project.addedField); | ||
// @ts-expect-error - `dep` is missing | ||
() => garn.mkProject({ description: "" }, {}).add(plugin); | ||
}); | ||
|
||
it("allows overwriting fields", () => { | ||
const plugin: Plugin<{ field: Package }> = (p) => ({ | ||
field: garn.build``, | ||
}); | ||
const project = garn | ||
.mkProject( | ||
{ description: "", defaultEnvironment: garn.emptyEnvironment }, | ||
{ field: garn.shell("") }, | ||
) | ||
.addExecutable("foo", "") | ||
.add(plugin); | ||
assertTypeIsExecutable(project.foo); | ||
// @ts-expect-error - should not be an `Executable` anymore | ||
assertTypeIsExecutable(project.field); | ||
assertTypeIsPackage(project.field); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.