Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ts/packages/git-story/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

module.exports = require("../../jest.config.js");
5 changes: 5 additions & 0 deletions ts/packages/git-story/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
"scripts": {
"build": "npm run tsc",
"clean": "rimraf --glob dist *.tsbuildinfo *.done.build.log",
"jest-esm": "node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js",
"prettier": "prettier --check . --ignore-path ../../.prettierignore",
"prettier:fix": "prettier --write . --ignore-path ../../.prettierignore",
"test": "npm run test:local",
"test:local": "pnpm run jest-esm --testPathPattern=\".*[.]spec[.]js\"",
"tsc": "tsc -b"
},
"dependencies": {
"commander": "^12.1.0"
},
"devDependencies": {
"@types/jest": "^29.5.7",
"@types/node": "^20.10.0",
"jest": "^29.7.0",
"rimraf": "^6.0.1",
"typescript": "~5.4.5"
}
Expand Down
56 changes: 56 additions & 0 deletions ts/packages/git-story/test/hooks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";

const CLI = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../cli.js",
);

// End to end: `init` writes the hook, then a real `git commit` runs it
// through git's own sh (Git Bash on Windows). An extensionless sh shim puts
// `git-story` on PATH, same as `npm link` does on every OS.
test("pre-commit hook forwards args and stdin to git-story", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "git-story-"));
const bin = path.join(dir, "bin");
const repo = path.join(dir, "repo");
fs.mkdirSync(bin);
const cli = CLI.replace(/\\/g, "/");
fs.writeFileSync(
path.join(bin, "git-story"),
`#!/bin/sh\nexec node "${cli}" "$@"\n`,
{ mode: 0o755 },
);
const env = {
...process.env,
PATH: `${bin}${path.delimiter}${process.env.PATH}`,
GIT_CONFIG_GLOBAL: path.join(dir, "gitconfig"),
GIT_CONFIG_NOSYSTEM: "1",
};
// Returns stdout + stderr; git sends hook output to stderr.
const run = (cmd: string, args: string[], input = "") => {
const r = spawnSync(cmd, args, {
cwd: repo,
env,
input,
encoding: "utf8",
});
expect(r.status).toBe(0);
return r.stdout + r.stderr;
};
fs.mkdirSync(repo);
run("git", ["init", "-q"]);
run("node", [CLI, "init"]);
const commit = ["-c", "user.name=t", "-c", "user.email=t@t"];
const out = run("git", [...commit, "commit", "--allow-empty", "-m", "x"]);
expect(out).toContain('git-story pre-commit: args=[] stdin=""');

const hook = path.join(repo, ".git/hooks/pre-commit");
const direct = run("sh", [hook, "a"], "piped\n");
expect(direct).toContain('args=["a"] stdin="piped\\n"');
});
14 changes: 14 additions & 0 deletions ts/packages/git-story/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": ".",
"outDir": "../dist/test",
"types": ["node", "jest"]
},
"include": ["./**/*"],
"ts-node": {
"esm": true
},
"references": [{ "path": "../src" }]
}
3 changes: 3 additions & 0 deletions ts/packages/git-story/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"references": [
{
"path": "./src"
},
{
"path": "./test"
}
],
"ts-node": {
Expand Down
6 changes: 6 additions & 0 deletions ts/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading