mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): the skills update check never reads a project's git config (#7975)
This commit is contained in:
@@ -2,7 +2,7 @@ import { expect, it, vi } from "vitest";
|
||||
|
||||
const checks = vi.hoisted(() => ({
|
||||
checkForUpdate: vi.fn(async () => ({})),
|
||||
refreshSkillsCache: vi.fn(async () => {
|
||||
refreshSkillsCache: vi.fn(async (_projectDir?: string) => {
|
||||
throw new Error("offline");
|
||||
}),
|
||||
}));
|
||||
@@ -10,18 +10,19 @@ vi.mock("./utils/updateCheck.js", () => ({ checkForUpdate: checks.checkForUpdate
|
||||
vi.mock("./utils/skillsUpdateCheck.js", () => ({ refreshSkillsCache: checks.refreshSkillsCache }));
|
||||
|
||||
it("runs each check it was handed, and one failing does not stop the other", async () => {
|
||||
process.argv = [process.execPath, "backgroundChecksWorker.js", "skills", "update"];
|
||||
process.argv = [process.execPath, "backgroundChecksWorker.js", "/work/film", "skills", "update"];
|
||||
|
||||
await import("./backgroundChecksWorker.js");
|
||||
|
||||
expect(checks.refreshSkillsCache).toHaveBeenCalledTimes(1);
|
||||
expect(checks.refreshSkillsCache).toHaveBeenCalledWith("/work/film");
|
||||
expect(checks.checkForUpdate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("skips a check the parent did not find due", async () => {
|
||||
vi.resetModules();
|
||||
checks.checkForUpdate.mockClear();
|
||||
process.argv = [process.execPath, "backgroundChecksWorker.js", "skills"];
|
||||
process.argv = [process.execPath, "backgroundChecksWorker.js", "/work/film", "skills"];
|
||||
|
||||
await import("./backgroundChecksWorker.js");
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ import { checkForUpdate } from "./utils/updateCheck.js";
|
||||
import { refreshSkillsCache } from "./utils/skillsUpdateCheck.js";
|
||||
|
||||
// Detached child of launchBackgroundChecks: refreshes the caches the next run's notices read.
|
||||
const due = process.argv.slice(2);
|
||||
const [projectDir, ...due] = process.argv.slice(2);
|
||||
await Promise.allSettled([
|
||||
due.includes("update") && checkForUpdate(),
|
||||
due.includes("skills") && refreshSkillsCache(),
|
||||
due.includes("skills") && refreshSkillsCache(projectDir),
|
||||
]);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { basename } from "node:path";
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { basename, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
@@ -26,7 +27,7 @@ const { launchBackgroundChecks } = await import("./backgroundChecks.js");
|
||||
/** The checks handed to the child, or null when none was spawned. */
|
||||
function spawnedChecks(): string[] | null {
|
||||
const call = spawn.mock.calls[0] as unknown as [string, string[]] | undefined;
|
||||
return call ? call[1].slice(1) : null;
|
||||
return call ? call[1].slice(2) : null;
|
||||
}
|
||||
|
||||
/** tsup entry name -> source file, as the package build declares them. */
|
||||
@@ -70,6 +71,20 @@ describe("launchBackgroundChecks", () => {
|
||||
expect(child.on).toHaveBeenCalledWith("error", expect.any(Function));
|
||||
});
|
||||
|
||||
it("starts the child in the CLI's own folder and names the project folder only as its argument", () => {
|
||||
const configDir = join(mkdtempSync(join(tmpdir(), "bg-checks-")), "config");
|
||||
vi.stubEnv("HYPERFRAMES_CONFIG_DIR", configDir);
|
||||
try {
|
||||
launchBackgroundChecks();
|
||||
const [, args, opts] = spawn.mock.calls[0] as unknown as [string, string[], { cwd?: string }];
|
||||
expect(opts.cwd).toBe(configDir);
|
||||
expect(existsSync(configDir)).toBe(true);
|
||||
expect(args[1]).toBe(process.cwd());
|
||||
} finally {
|
||||
rmSync(join(configDir, ".."), { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("spawns the file the package build emits", () => {
|
||||
launchBackgroundChecks();
|
||||
const worker = (spawn.mock.calls[0] as unknown as [string, string[]])[1][0]!;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { readConfig, readConfigFresh, writeConfig } from "../telemetry/config.js";
|
||||
import { hyperframesConfigDir } from "./configDir.js";
|
||||
import { skillsCheckDue } from "./skillsUpdateCheck.js";
|
||||
import { updateCheckDue } from "./updateCheck.js";
|
||||
|
||||
@@ -31,7 +33,11 @@ export function launchBackgroundChecks(): void {
|
||||
// Next to the bundled cli.js; from source (dev mode) no check is ever due.
|
||||
const worker = join(dirname(fileURLToPath(import.meta.url)), "backgroundChecksWorker.js");
|
||||
try {
|
||||
const child = spawn(process.execPath, [worker, ...due], {
|
||||
const cliFolder = hyperframesConfigDir();
|
||||
mkdirSync(cliFolder, { recursive: true, mode: 0o700 });
|
||||
const projectDir = process.cwd();
|
||||
const child = spawn(process.execPath, [worker, projectDir, ...due], {
|
||||
cwd: cliFolder,
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
windowsHide: true,
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// The skills check's `git ls-remote` never reads a repository's config from the folder it runs in.
|
||||
// Real git, offline: the person's own config sends all of GitHub to a local "honest" copy, and the
|
||||
// project's repo config (bare layout, or a .git above the folder) sends this one repo, a longer and
|
||||
// so winning rule, to a "planted" one.
|
||||
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { checkSkills, MANIFEST_FILE } from "./skillsManifest.js";
|
||||
|
||||
const REMOTE = "https://github.com/heygen-com/hyperframes.git";
|
||||
const RAW = "https://raw.githubusercontent.com/heygen-com/hyperframes";
|
||||
|
||||
let root: string;
|
||||
let startDir: string;
|
||||
|
||||
const git = (cwd: string, ...args: string[]) => {
|
||||
const { GIT_DIR: _, ...env } = process.env;
|
||||
return execFileSync("git", args, { cwd, encoding: "utf8", env }).trim();
|
||||
};
|
||||
|
||||
/** A repo whose main has one commit named for it; returns that commit. */
|
||||
function repoWithMain(dir: string): string {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
git(dir, "init", "-q", "-b", "main");
|
||||
git(dir, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", dir);
|
||||
return git(dir, "rev-parse", "HEAD");
|
||||
}
|
||||
|
||||
const rewrite = (configFile: string, to: string, from: string) =>
|
||||
git(root, "config", "--file", configFile, `url.${to}.insteadOf`, from);
|
||||
const sendGitHubTo = (configFile: string, repo: string) =>
|
||||
rewrite(configFile, pathToFileURL(repo).href, REMOTE);
|
||||
|
||||
const layouts: Record<string, (planted: string) => string> = {
|
||||
"a bare-layout repo": (planted) => {
|
||||
const project = join(root, "bare-project");
|
||||
mkdirSync(project);
|
||||
git(project, "init", "-q", "--bare", ".");
|
||||
sendGitHubTo(join(project, "config"), planted);
|
||||
return project;
|
||||
},
|
||||
"a folder inside a repo": (planted) => {
|
||||
const repo = join(root, "repo");
|
||||
mkdirSync(repo);
|
||||
git(repo, "init", "-q");
|
||||
sendGitHubTo(join(repo, ".git", "config"), planted);
|
||||
const project = join(repo, "film");
|
||||
mkdirSync(project);
|
||||
return project;
|
||||
},
|
||||
// As in a command run from one of the project's git hooks.
|
||||
"a repo an inherited GIT_DIR names": (planted) => {
|
||||
const repo = join(root, "hooked");
|
||||
mkdirSync(repo);
|
||||
git(repo, "init", "-q");
|
||||
sendGitHubTo(join(repo, ".git", "config"), planted);
|
||||
vi.stubEnv("GIT_DIR", join(repo, ".git"));
|
||||
const project = join(root, "film");
|
||||
mkdirSync(project);
|
||||
return project;
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
root = mkdtempSync(join(tmpdir(), "skills-git-folder-"));
|
||||
startDir = process.cwd();
|
||||
vi.stubEnv("GIT_CONFIG_NOSYSTEM", "1");
|
||||
vi.stubEnv("GIT_CONFIG_GLOBAL", join(root, "person.gitconfig"));
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => ({ ok: true, status: 200, json: async () => ({ skills: {} }) })),
|
||||
);
|
||||
});
|
||||
afterEach(() => {
|
||||
process.chdir(startDir);
|
||||
vi.unstubAllEnvs();
|
||||
vi.unstubAllGlobals();
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("the skills check's main lookup", () => {
|
||||
it.each(Object.keys(layouts))("reads no git config from %s", async (layout) => {
|
||||
const honest = repoWithMain(join(root, "github", "heygen-com", "hyperframes.git"));
|
||||
const planted = repoWithMain(join(root, "planted"));
|
||||
const github = `${pathToFileURL(join(root, "github")).href}/`;
|
||||
rewrite(join(root, "person.gitconfig"), github, "https://github.com/");
|
||||
expect(planted).not.toBe(honest);
|
||||
const project = layouts[layout]!(join(root, "planted"));
|
||||
process.chdir(project);
|
||||
|
||||
// A prerelease has no release tag, so the check pins main through git.
|
||||
await checkSkills({
|
||||
home: join(root, "home"),
|
||||
canonical: true,
|
||||
cliVersion: "0.9.59-alpha.1",
|
||||
});
|
||||
|
||||
const fetched = vi.mocked(fetch).mock.calls.map(([url]) => String(url));
|
||||
expect(fetched).toEqual([`${RAW}/${honest}/${MANIFEST_FILE}`]);
|
||||
expect(fetched.join(" ")).not.toContain(planted);
|
||||
});
|
||||
});
|
||||
@@ -17,8 +17,8 @@
|
||||
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
@@ -633,10 +633,13 @@ async function fetchManifest(url: string): Promise<SkillsManifest> {
|
||||
* Credential Manager (the Git for Windows default) answers a private or missing
|
||||
* repo's auth challenge with a blocking sign-in window. An empty credential.helper
|
||||
* clears every helper; an empty GIT_ASKPASS also skips core.askPass and SSH_ASKPASS.
|
||||
* Git's folder and repository are an empty folder of its own, so no project's repo config is read.
|
||||
*/
|
||||
async function remoteHeadSha(repoSlug: string): Promise<string | null> {
|
||||
if (!REPO_SLUG_PATTERN.test(repoSlug)) return null;
|
||||
let noRepo: string | undefined;
|
||||
try {
|
||||
noRepo = mkdtempSync(join(tmpdir(), "hyperframes-git-"));
|
||||
const { stdout } = await execFileAsync(
|
||||
"git",
|
||||
[
|
||||
@@ -648,8 +651,10 @@ async function remoteHeadSha(repoSlug: string): Promise<string | null> {
|
||||
],
|
||||
{
|
||||
timeout: FETCH_TIMEOUT_MS,
|
||||
cwd: noRepo,
|
||||
env: {
|
||||
...process.env,
|
||||
GIT_DIR: noRepo,
|
||||
GIT_TERMINAL_PROMPT: "0",
|
||||
GIT_ASKPASS: "",
|
||||
GCM_INTERACTIVE: "never",
|
||||
@@ -661,6 +666,8 @@ async function remoteHeadSha(repoSlug: string): Promise<string | null> {
|
||||
return /^[0-9a-f]{40}$/.test(sha) ? sha : null;
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
if (noRepo) rmSync(noRepo, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,12 +39,12 @@ function cacheFresh(config: ReturnType<typeof readConfig>, now: number): boolean
|
||||
}
|
||||
|
||||
/** Run the real check and persist the result to the cache. Skipped when DNS does not answer. */
|
||||
export async function refreshSkillsCache(): Promise<void> {
|
||||
export async function refreshSkillsCache(projectDir?: string): Promise<void> {
|
||||
if (!(await hostAnswers("github.com"))) return;
|
||||
// `canonical: true` so this nudge's counts agree with `updateSkills`'s
|
||||
// source of truth — otherwise a stale in-repo skills-manifest.json (e.g.
|
||||
// inside a hyperframes checkout) can produce a false-positive count here.
|
||||
recordSkillsCheck(await checkSkills({ canonical: true, cliVersion: VERSION }));
|
||||
recordSkillsCheck(await checkSkills({ canonical: true, cliVersion: VERSION, cwd: projectDir }));
|
||||
}
|
||||
|
||||
/** Cache a canonical skills check (at this CLI's tag) for the nudge and `_meta.skillsOutdated`. */
|
||||
|
||||
Reference in New Issue
Block a user