Repository navigation
fix(init): finish the review follow-ups for the Android starter #641
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test"; | ||
| import { useCaptureLog } from "../../../test/lib/stubs.ts"; | ||
| import { readdir, readFile, rm, stat } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
@@ -70,6 +71,39 @@ describe("starterProjectFiles", () => { | |
| }); | ||
|
|
||
| describe("writeAndroidStarter", () => { | ||
| const captured = useCaptureLog(); | ||
| // Git needs an identity to commit and a CI runner has none; the starter passes process.env on. | ||
| const IDENTITY = { | ||
| GIT_AUTHOR_NAME: "t", | ||
| GIT_AUTHOR_EMAIL: "t@t", | ||
| GIT_COMMITTER_NAME: "t", | ||
| GIT_COMMITTER_EMAIL: "t@t", | ||
| }; | ||
| const NO_IDENTITY = { | ||
| GIT_AUTHOR_NAME: undefined, | ||
| GIT_COMMITTER_NAME: undefined, | ||
| // An empty configured name is refused, and stops Git guessing one from the host. | ||
| GIT_CONFIG_COUNT: "1", | ||
| GIT_CONFIG_KEY_0: "user.name", | ||
| GIT_CONFIG_VALUE_0: "", | ||
| }; | ||
| let restoreEnv = () => {}; | ||
| function setEnv(values: Record<string, string | undefined>) { | ||
| const saved = Object.entries(values).map(([key]) => [key, process.env[key]] as const); | ||
| const apply = (entries: Iterable<readonly [string, string | undefined]>) => { | ||
| for (const [key, value] of entries) { | ||
| if (value === undefined) delete process.env[key]; | ||
| else process.env[key] = value; | ||
| } | ||
| }; | ||
| apply(Object.entries(values)); | ||
| return () => apply(saved); | ||
| } | ||
| beforeAll(() => { | ||
| restoreEnv = setEnv(IDENTITY); | ||
| }); | ||
| afterAll(() => restoreEnv()); | ||
|
|
||
| async function write(keep: string[] = roots) { | ||
| const cwd = await mkdtemp(join(tmpdir(), "clerk-android-starter-")); | ||
| keep.push(cwd); | ||
|
|
@@ -94,20 +128,42 @@ describe("writeAndroidStarter", () => { | |
| }); | ||
| }); | ||
|
|
||
| test("writes the executable wrapper and an initial commit", async () => { | ||
| // Windows has no executable bit to set or report. | ||
| test.skipIf(process.platform === "win32")( | ||
| "writes the wrapper executable, in the folder and in the initial commit", | ||
| async () => { | ||
| const dir = await write(); | ||
| expect((await stat(join(dir, "gradlew"))).mode & 0o777).toBe(0o755); | ||
| expect((await stat(join(dir, "gradlew.bat"))).mode & 0o777).toBe(0o644); | ||
| // The initial commit records the executable bit, so a clone can run ./gradlew too. | ||
| const index = await Bun.$`git ls-files -s gradlew`.cwd(dir).quiet().nothrow(); | ||
| expect(index.stdout.toString()).toStartWith("100755 "); | ||
| }, | ||
| ); | ||
|
|
||
| test("writes the wrapper files and an initial commit", async () => { | ||
| const dir = await write(); | ||
| expect((await stat(join(dir, "gradlew"))).mode & 0o777).toBe(0o755); | ||
| expect((await stat(join(dir, "gradlew.bat"))).mode & 0o777).toBe(0o644); | ||
| // The initial commit records the executable bit, so a clone can run ./gradlew too. | ||
| const index = await Bun.$`git ls-files -s gradlew`.cwd(dir).quiet().nothrow(); | ||
| expect(index.stdout.toString()).toStartWith("100755 "); | ||
| expect((await stat(join(dir, "gradle/wrapper/gradle-wrapper.jar"))).size).toBeGreaterThan( | ||
| 40_000, | ||
| ); | ||
| expect(await readFile(join(dir, "gradlew.bat"), "utf8")).toContain("gradle-wrapper.jar"); | ||
| const log = await Bun.$`git log --oneline`.cwd(dir).quiet().nothrow(); | ||
| expect(log.stdout.toString()).toContain("Initial commit"); | ||
| expect((await Bun.$`git status --porcelain`.cwd(dir).quiet()).stdout.toString()).toBe(""); | ||
| expect(captured.err).not.toContain("initial commit"); | ||
| }); | ||
|
|
||
| test("warns when Git can't make the initial commit, and still writes the project", async () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this covers the warning nicely. Its counterpart is that a missing Git stays silent, as the PR says. Could the "without Git on PATH" test just below also assert |
||
| const restore = setEnv(NO_IDENTITY); | ||
| try { | ||
| const dir = await write(); | ||
| expect(await readdir(dir)).toContain("app"); | ||
| expect(captured.err).toContain("Git couldn't make the new app's initial commit"); | ||
| const log = await Bun.$`git log --oneline`.cwd(dir).quiet().nothrow(); | ||
| expect(log.stdout.toString()).toBe(""); | ||
| } finally { | ||
| restore(); | ||
| } | ||
| }); | ||
|
|
||
| test("writes the project without Git on PATH, just with no repository", async () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { dirname, join } from "node:path"; | |
| import wrapperJar from "./starter/gradle-wrapper.jar" with { type: "file" }; | ||
| import gradlewScript from "./starter/gradlew" with { type: "file" }; | ||
| import gradlewBatch from "./starter/gradlew.bat" with { type: "file" }; | ||
| import { log } from "../../../lib/log.ts"; | ||
| import { createFile } from "./files.ts"; | ||
| import { CLERK_ANDROID_MIN_SDK } from "./sdk.ts"; | ||
|
|
||
|
|
@@ -477,15 +478,23 @@ async function initializeRepository(directory: string): Promise<void> { | |
| cwd: directory, | ||
| env: process.env, | ||
| stdout: "ignore", | ||
| stderr: "ignore", | ||
| stderr: "pipe", | ||
| timeout: 15_000, | ||
| }); | ||
| return (await child.exited) === 0; | ||
| const [code, stderr] = await Promise.all([child.exited, new Response(child.stderr).text()]); | ||
| if (code !== 0) | ||
| log.debug(`git: ${args.join(" ")} exited ${code} in ${directory}: ${stderr.trim()}`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: with no identity, Git's stderr is the roughly 10-line "Please tell me who you are" block, so this one |
||
| return code === 0; | ||
| }; | ||
| try { | ||
| if (!(await run(["init", "-q"]))) return; | ||
| if (!(await run(["add", "-A"]))) return; | ||
| await run(["-c", "commit.gpgsign=false", "commit", "-q", "-m", "Initial commit"]); | ||
| // Staged but uncommitted files aren't clean to the setup that follows, so it keeps backups of | ||
| // the ones it edits; say why, since nothing else does. | ||
| if (!(await run(["-c", "commit.gpgsign=false", "commit", "-q", "-m", "Initial commit"]))) | ||
| log.warn( | ||
| "Git couldn't make the new app's initial commit, so its files are staged but not committed. If Git has no identity here, set user.name and user.email, then commit them.", | ||
| ); | ||
| } catch { | ||
| // Git isn't installed. | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small one: any Android flag selects the starter, not just
--android-package(clerk init --starter --android-sdk uipicks Android too, viaframeworkOverride). Maybe "as does any Android flag on its own, such as--android-package"?