-
Notifications
You must be signed in to change notification settings - Fork 7
Implement importFlake #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a93dc26
Implement importFlake
alexdavid 61a979f
Address PR comments
alexdavid f06c4d1
Use buildPackage for `getPackage` tests
alexdavid 3c05d0c
Pull in runInDevShell from #466
alexdavid 1cb9af6
Separate callFlake from importFlake and add jsdoc
alexdavid 788a0bd
Merge branch 'main' into ad/call-flake-dogfooding-poc
alexdavid 1968c75
Update mod.ts export
alexdavid 9bcd588
Review comments
soenkehahn cf3f9af
Merge remote-tracking branch 'origin/main' into ad/call-flake-dogfood…
soenkehahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,288 @@ | ||
| import { | ||
| afterEach, | ||
| beforeEach, | ||
| describe, | ||
| it, | ||
| } from "https://deno.land/std@0.206.0/testing/bdd.ts"; | ||
| import { | ||
| assertStderrContains, | ||
| assertStdout, | ||
| assertSuccess, | ||
| assertThrowsWith, | ||
| buildPackage, | ||
| runCheck, | ||
| runCommand, | ||
| runExecutable, | ||
| runInDevShell, | ||
| } from "./testUtils.ts"; | ||
| import { callFlake, importFlake, importFromGithub } from "./importFlake.ts"; | ||
| import * as garn from "./mod.ts"; | ||
| import { existsSync } from "https://deno.land/std@0.201.0/fs/mod.ts"; | ||
| import { assertEquals } from "https://deno.land/std@0.206.0/assert/mod.ts"; | ||
|
|
||
| describe("callFlake", () => { | ||
| let tempDir: string; | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = Deno.makeTempDirSync({ prefix: "garn-test" }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Deno.removeSync(tempDir, { recursive: true }); | ||
| }); | ||
|
|
||
| const writeFlakeToImport = (contents: string) => { | ||
| Deno.mkdirSync(`${tempDir}/to-import`, { recursive: true }); | ||
| Deno.writeTextFileSync( | ||
| `${tempDir}/to-import/flake.nix`, | ||
| ` | ||
| { | ||
| inputs.nixpkgs.url = "github:NixOS/nixpkgs/6fc7203e423bbf1c8f84cccf1c4818d097612566"; | ||
| inputs.flake-utils.url = "github:numtide/flake-utils/ff7b65b44d01cf9ba6a71320833626af21126384"; | ||
| outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: | ||
| let pkgs = import "\${nixpkgs}" { inherit system; }; in ${contents} | ||
| ); | ||
| } | ||
| `, | ||
| ); | ||
| if (!existsSync(`${tempDir}/to-import/flake.lock`)) { | ||
| assertSuccess( | ||
| runCommand( | ||
| new Deno.Command("nix", { | ||
| args: ["flake", "lock"], | ||
| cwd: `${tempDir}/to-import`, | ||
| }), | ||
| ), | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| describe("getPackage", () => { | ||
| it("returns the specified package from the flake file", () => { | ||
| writeFlakeToImport(`{ | ||
| packages = { | ||
| main_pkg = pkgs.runCommand "create-some-files" {} '' | ||
| mkdir $out | ||
| touch $out/foo $out/bar | ||
| ''; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const pkg = buildPackage(flake.getPackage("main_pkg"), { dir: tempDir }); | ||
| assertEquals(getDirEntryNames(pkg), ["bar", "foo"]); | ||
| }); | ||
|
|
||
| it("reflects changes when changing the flake source after importing", () => { | ||
| writeFlakeToImport(`{ | ||
| packages = { | ||
| main_pkg = pkgs.runCommand "create-some-files" {} '' | ||
| mkdir $out | ||
| touch $out/original | ||
| ''; | ||
| }; | ||
| }`); | ||
| const flake = callFlake(`./to-import`); | ||
| buildPackage(flake.getPackage("main_pkg"), { dir: tempDir }); | ||
| writeFlakeToImport(`{ | ||
| packages = { | ||
| main_pkg = pkgs.runCommand "create-some-files" {} '' | ||
| mkdir $out | ||
| touch $out/modified | ||
| ''; | ||
| }; | ||
| }`); | ||
| const pkg = buildPackage(flake.getPackage("main_pkg"), { dir: tempDir }); | ||
| assertEquals(getDirEntryNames(pkg), ["modified"]); | ||
| }); | ||
|
|
||
| it("allows importing relative sources in packages", () => { | ||
| Deno.writeTextFileSync( | ||
| `${tempDir}/foo.js`, | ||
| "console.log('Hello from a js file in tempDir!')", | ||
| ); | ||
| writeFlakeToImport(`{ | ||
| packages = { | ||
| foo = pkgs.writeScript "foo" '' | ||
| \${pkgs.nodejs}/bin/node \${../foo.js} | ||
| ''; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const exe = garn.shell`${flake.getPackage("foo")}`; | ||
alexdavid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const output = assertSuccess(runExecutable(exe, { cwd: tempDir })); | ||
| assertStdout(output, "Hello from a js file in tempDir!\n"); | ||
| }); | ||
|
|
||
| it("displays a helpful error if the specified package does not exist", () => { | ||
| writeFlakeToImport(`{ packages = { }; }`); | ||
| const flake = callFlake("./to-import"); | ||
| const exe = garn.shell`${flake.getPackage("foo")}`; | ||
| const output = runExecutable(exe, { cwd: tempDir }); | ||
| assertStderrContains( | ||
| output, | ||
| 'error: The package "foo" was not found in ./to-import', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getApp", () => { | ||
| it("returns the specified executable from the flake file apps", () => { | ||
| writeFlakeToImport(`{ | ||
| apps = { | ||
| hello = { | ||
| type = "app"; | ||
| program = builtins.toString (pkgs.writeScript "hello" '' | ||
| echo hello from flake file | ||
| ''); | ||
| }; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const exe = flake.getApp("hello"); | ||
| const output = assertSuccess(runExecutable(exe, { cwd: tempDir })); | ||
| assertStdout(output, "hello from flake file\n"); | ||
| }); | ||
|
|
||
| it("displays a helpful error if the specified app does not exist", () => { | ||
| writeFlakeToImport(`{ apps = { }; }`); | ||
| const flake = callFlake("./to-import"); | ||
| const exe = garn.shell`${flake.getApp("foo")}`; | ||
| const output = runExecutable(exe, { cwd: tempDir }); | ||
| assertStderrContains( | ||
| output, | ||
| 'error: The app "foo" was not found in ./to-import', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getCheck", () => { | ||
| it("returns the specified check from the flake file", () => { | ||
| writeFlakeToImport(`{ | ||
| checks = { | ||
| some-check = pkgs.runCommand "my-check" {} '' | ||
| # ${Date.now()} | ||
| touch $out | ||
| echo running my-check! | ||
| ''; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const check = flake.getCheck("some-check"); | ||
| const output = assertSuccess(runCheck(check, { dir: tempDir })); | ||
| assertStderrContains(output, "running my-check!"); | ||
| }); | ||
|
|
||
| it("displays a helpful error if the specified check does not exist", () => { | ||
| writeFlakeToImport(`{ checks = { }; }`); | ||
| const flake = callFlake("./to-import"); | ||
| const exe = garn.shell`${flake.getCheck("foo")}`; | ||
| const output = runExecutable(exe, { cwd: tempDir }); | ||
| assertStderrContains( | ||
| output, | ||
| 'error: The check "foo" was not found in ./to-import', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getDevShell", () => { | ||
| it("returns the specified environment from the flake file", () => { | ||
| writeFlakeToImport(`{ | ||
| devShells = { | ||
| some-shell = pkgs.mkShell { | ||
| nativeBuildInputs = [ pkgs.hello ]; | ||
| }; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const env = flake.getDevShell("some-shell"); | ||
| const output = runInDevShell(env, { cmd: "hello", dir: tempDir }); | ||
| assertStdout(output, "Hello, world!\n"); | ||
| }); | ||
|
|
||
| it("displays a helpful error if the specified devShell does not exist", () => { | ||
| writeFlakeToImport(`{ devShells = { }; }`); | ||
| const flake = callFlake("./to-import"); | ||
| const exe = garn.shell`${flake.getDevShell("foo")}`; | ||
| const output = runExecutable(exe, { cwd: tempDir }); | ||
| assertStderrContains( | ||
| output, | ||
| 'error: The devShell "foo" was not found in ./to-import', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("allPackages", () => { | ||
| it("returns a package with symlinks to all found packages", () => { | ||
| writeFlakeToImport(`{ | ||
| packages = { | ||
| foo = pkgs.runCommand "foo" {} "echo foo-pkg > $out"; | ||
| bar = pkgs.runCommand "bar" {} "echo bar-pkg > $out"; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const pkg = buildPackage(flake.allPackages, { dir: tempDir }); | ||
| assertEquals(getDirEntryNames(pkg), ["bar", "foo"]); | ||
| assertEquals(Deno.readTextFileSync(`${pkg}/foo`), "foo-pkg\n"); | ||
| assertEquals(Deno.readTextFileSync(`${pkg}/bar`), "bar-pkg\n"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getAllChecks", () => { | ||
| it("returns a check that composes all found checks", () => { | ||
| writeFlakeToImport(`{ | ||
| checks = { | ||
| foo = pkgs.runCommand "check-foo" {} '' | ||
| # ${Date.now()} | ||
| touch $out | ||
| echo running check foo | ||
| ''; | ||
| bar = pkgs.runCommand "check-bar" {} '' | ||
| # ${Date.now()} | ||
| touch $out | ||
| echo running check bar | ||
| ''; | ||
| }; | ||
| }`); | ||
| const flake = callFlake("./to-import"); | ||
| const output = assertSuccess(runCheck(flake.allChecks, { dir: tempDir })); | ||
| assertStderrContains(output, "running check foo"); | ||
| assertStderrContains(output, "running check bar"); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("importFlake", () => { | ||
| it("allows importing flakes from url", () => { | ||
| const flake = importFlake( | ||
| "github:garnix-io/debug-tools/8a4026fa6ccbfec070f96d458ffa96e7fb6112e8", | ||
| ); | ||
| const exe = flake.getPackage("main_pkg").bin("debug-args"); | ||
| const output = assertSuccess(runExecutable(exe)); | ||
| assertStdout(output, ""); | ||
| assertStderrContains(output, "[]"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("importFromGithub", () => { | ||
| it("allows importing flakes from GitHub repositories", () => { | ||
| const flake = importFromGithub({ | ||
| repo: "garnix-io/debug-tools", | ||
| revOrRef: "8a4026fa6ccbfec070f96d458ffa96e7fb6112e8", | ||
| }); | ||
| const exe = flake.getPackage("main_pkg").bin("debug-args"); | ||
| const output = assertSuccess(runExecutable(exe)); | ||
| assertStdout(output, ""); | ||
| assertStderrContains(output, "[]"); | ||
| }); | ||
|
|
||
| it("throws an error if the repo is malformed", () => { | ||
| assertThrowsWith( | ||
| "The `repo` of a hosted git service should match <owner>/<repo>", | ||
| () => importFromGithub({ repo: "/foo/bar" }), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| function getDirEntryNames(path: string) { | ||
| return [...Deno.readDirSync(path)].map((entry) => entry.name).sort(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.