-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Handles persisted directories in between test runs. Should improve debuggability icm with the `--debug` flag - Supports sending inputs - Collects the resulting stdout to match on. - Feels somewhat overkill to snapshot output + directory state for now. May do that at some point.
- Loading branch information
Showing
7 changed files
with
658 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { existsSync } from "node:fs"; | ||
import { readdir } from "node:fs/promises"; | ||
import { mainTestFn, test } from "@compas/cli"; | ||
import { pathJoin } from "@compas/stdlib"; | ||
import { testCompasCli, testDirectory } from "./utils.js"; | ||
|
||
mainTestFn(import.meta); | ||
|
||
test("compas/cli", (t) => { | ||
t.jobs = 4; | ||
|
||
const workingDirectory = testDirectory(t.name); | ||
|
||
t.test("does not create a debug file without --debug", async (t) => { | ||
const cwd = workingDirectory("no-debug"); | ||
|
||
await testCompasCli({ | ||
args: ["foo"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
t.equal(existsSync(pathJoin(cwd, ".cache/compas")), false); | ||
}); | ||
|
||
t.test("creates a debug file with --debug", async (t) => { | ||
const cwd = workingDirectory("with-debug"); | ||
|
||
await testCompasCli({ | ||
args: ["foo", "--debug"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
t.equal(existsSync(pathJoin(cwd, ".cache/compas")), true); | ||
t.equal( | ||
(await readdir(pathJoin(cwd, ".cache/compas"), {})).some( | ||
(it) => it.startsWith("debug-") && it.endsWith(".txt"), | ||
), | ||
true, | ||
); | ||
}); | ||
|
||
t.test("package.json is not available", async (t) => { | ||
const cwd = workingDirectory("no-package-json"); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: [], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
t.ok( | ||
stdout.includes("Please run 'npx compas@latest init' to install Compas."), | ||
); | ||
}); | ||
|
||
t.test("unsupported command", async (t) => { | ||
const cwd = workingDirectory("unknown-command"); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["foo"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
t.ok(stdout.includes(`Unsupported command. Available commands:`)); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,242 @@ | ||
import { existsSync } from "node:fs"; | ||
import { mkdir, readdir, readFile } from "node:fs/promises"; | ||
import { mainTestFn, test } from "@compas/cli"; | ||
import { dirnameForModule, pathJoin } from "@compas/stdlib"; | ||
import { writeFileChecked } from "../../src/shared/fs.js"; | ||
import { testCompasCli, testDirectory } from "../utils.js"; | ||
|
||
mainTestFn(import.meta); | ||
|
||
test("compas/commands/init", (t) => { | ||
t.jobs = 4; | ||
|
||
const workingDirectory = testDirectory(t.name); | ||
|
||
t.test("exits in CI mode", async (t) => { | ||
const cwd = workingDirectory("no-ci"); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["init"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
env: { | ||
...process.env, | ||
CI: "true", | ||
}, | ||
}); | ||
|
||
t.ok(stdout.includes("'compas init' is not supported in CI.")); | ||
}); | ||
|
||
t.test("new project", async (t) => { | ||
const cwd = workingDirectory("new-project"); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["init"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
// Package.json | ||
t.ok( | ||
existsSync(pathJoin(cwd, "package.json")), | ||
"Should create a package.json", | ||
); | ||
t.deepEqual( | ||
JSON.parse(await readFile(pathJoin(cwd, "package.json"), "utf-8")), | ||
{ | ||
name: "new-project", | ||
private: true, | ||
version: "0.0.1", | ||
type: "module", | ||
scripts: {}, | ||
dependencies: { | ||
compas: JSON.parse( | ||
await readFile( | ||
pathJoin(dirnameForModule(import.meta), "../../package.json"), | ||
"utf-8", | ||
), | ||
).version, | ||
}, | ||
}, | ||
); | ||
|
||
// Package manager | ||
t.ok(stdout.includes("_compas_skip_package_manager_install")); | ||
|
||
// Git | ||
t.ok( | ||
existsSync(pathJoin(cwd, ".gitignore")), | ||
".gitignore should've been created", | ||
); | ||
t.ok(existsSync(pathJoin(cwd, ".git")), "Git repo should've been created"); | ||
|
||
// Output | ||
t.ok(stdout.includes("'npx compas'")); | ||
}); | ||
|
||
t.test("new project - already .git", async (t) => { | ||
const cwd = workingDirectory("new-project-already-git"); | ||
|
||
await mkdir(pathJoin(cwd, ".git")); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["init"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
// Package.json | ||
t.ok( | ||
existsSync(pathJoin(cwd, "package.json")), | ||
"Should create a package.json", | ||
); | ||
|
||
// Package manager | ||
t.ok(stdout.includes("_compas_skip_package_manager_install")); | ||
|
||
// Git | ||
t.ok( | ||
existsSync(pathJoin(cwd, ".gitignore")), | ||
".gitignore should've been created", | ||
); | ||
t.equal( | ||
(await readdir(pathJoin(cwd, ".git"))).length, | ||
0, | ||
".git directory should be empty", | ||
); | ||
|
||
// Output | ||
t.ok(stdout.includes("'npx compas'")); | ||
}); | ||
|
||
t.test("exiting project - no dependencies", async (t) => { | ||
const cwd = workingDirectory("existing-project-no-deps"); | ||
|
||
await writeFileChecked(pathJoin(cwd, "package.json"), "{}"); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["init"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
// Package.json | ||
|
||
t.deepEqual( | ||
JSON.parse(await readFile(pathJoin(cwd, "package.json"), "utf-8")), | ||
{ | ||
dependencies: { | ||
compas: JSON.parse( | ||
await readFile( | ||
pathJoin(dirnameForModule(import.meta), "../../package.json"), | ||
"utf-8", | ||
), | ||
).version, | ||
}, | ||
}, | ||
); | ||
|
||
// Package manager | ||
t.ok(stdout.includes("Patching package.json")); | ||
t.ok(stdout.includes("_compas_skip_package_manager_install")); | ||
|
||
// Output | ||
t.ok(stdout.includes("Ready to roll!")); | ||
}); | ||
|
||
t.test("exiting project - no update", async (t) => { | ||
const cwd = workingDirectory("existing-project-no-update"); | ||
|
||
await writeFileChecked( | ||
pathJoin(cwd, "package.json"), | ||
JSON.stringify({ | ||
dependencies: { | ||
compas: JSON.parse( | ||
await readFile( | ||
pathJoin(dirnameForModule(import.meta), "../../package.json"), | ||
"utf-8", | ||
), | ||
).version, | ||
}, | ||
}), | ||
); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["init"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
// Package.json | ||
t.deepEqual( | ||
JSON.parse(await readFile(pathJoin(cwd, "package.json"), "utf-8")), | ||
{ | ||
dependencies: { | ||
compas: JSON.parse( | ||
await readFile( | ||
pathJoin(dirnameForModule(import.meta), "../../package.json"), | ||
"utf-8", | ||
), | ||
).version, | ||
}, | ||
}, | ||
); | ||
|
||
// Package manager | ||
t.ok( | ||
!stdout.includes("_compas_skip_package_manager_install"), | ||
"Package manager did run, but didn't need to", | ||
); | ||
|
||
// Output | ||
t.ok(stdout.includes("Already up-to-date!")); | ||
}); | ||
|
||
t.test("exiting project - update", async (t) => { | ||
const cwd = workingDirectory("existing-project-update"); | ||
|
||
await writeFileChecked( | ||
pathJoin(cwd, "package.json"), | ||
JSON.stringify({ | ||
dependencies: { | ||
compas: "*", | ||
}, | ||
}), | ||
); | ||
|
||
const { stdout } = await testCompasCli({ | ||
args: ["init"], | ||
inputs: [], | ||
waitForExit: true, | ||
cwd, | ||
}); | ||
|
||
// Package.json | ||
t.deepEqual( | ||
JSON.parse(await readFile(pathJoin(cwd, "package.json"), "utf-8")), | ||
{ | ||
dependencies: { | ||
compas: JSON.parse( | ||
await readFile( | ||
pathJoin(dirnameForModule(import.meta), "../../package.json"), | ||
"utf-8", | ||
), | ||
).version, | ||
}, | ||
}, | ||
); | ||
|
||
// Package manager | ||
t.ok(stdout.includes("Patching package.json")); | ||
t.ok(stdout.includes("_compas_skip_package_manager_install")); | ||
|
||
// Output | ||
t.ok(stdout.includes("Ready to roll!")); | ||
}); | ||
}); |
Oops, something went wrong.