-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@janfjohannes Are you sure the findRepoRoot / openRepository are working correctly in this case? Please check the test for the return values.
- Loading branch information
1 parent
b412a7b
commit cfbf4ec
Showing
12 changed files
with
998 additions
and
208 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
30 changes: 30 additions & 0 deletions
30
inlang/source-code/ide-extension/src/utilities/settings/_getGitOrigin.ts
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,30 @@ | ||
/** | ||
* Gets the git origin url of the currently opened repository. | ||
*/ | ||
|
||
import { findRepoRoot, openRepository } from "@lix-js/client" | ||
import type { NodeishFilesystem } from "@lix-js/fs" | ||
|
||
export async function _getGitOrigin({ | ||
fs, | ||
workspaceRoot, | ||
}: { | ||
fs: NodeishFilesystem | ||
workspaceRoot: string | undefined | ||
}) { | ||
try { | ||
const repoRoot = await findRepoRoot({ | ||
nodeishFs: fs, | ||
path: workspaceRoot || "", | ||
}) | ||
|
||
if (!repoRoot) { | ||
console.error("Failed to find repository root.") | ||
return | ||
} | ||
const repo = await openRepository(repoRoot, { nodeishFs: fs }) | ||
return repo.getOrigin() | ||
} catch (e) { | ||
return undefined | ||
} | ||
} |
50 changes: 19 additions & 31 deletions
50
inlang/source-code/ide-extension/src/utilities/settings/getGitOrigin.test.ts
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 |
---|---|---|
@@ -1,40 +1,28 @@ | ||
import { describe, it, expect, vi, beforeEach } from "vitest" | ||
import { getGitOrigin } from "./getGitOrigin.js" | ||
import * as lixClient from "@lix-js/client" | ||
import { describe, it, expect } from "vitest" | ||
import { createNodeishMemoryFs, fromSnapshot as loadSnapshot, type Snapshot } from "@lix-js/fs" | ||
import { readFileSync } from "node:fs" | ||
import { resolve } from "node:path" | ||
import { _getGitOrigin } from "./_getGitOrigin.js" | ||
|
||
// Mocks | ||
vi.mock("vscode", () => ({ | ||
workspace: { | ||
workspaceFolders: [{ uri: { fsPath: "mock/path" } }], | ||
}, | ||
})) | ||
vi.mock("@lix-js/client", () => ({ | ||
findRepoRoot: vi.fn().mockResolvedValue("mock/root"), | ||
_listRemotes: vi.fn().mockResolvedValue([{ url: "git@github.com:user/repo.git" }]), | ||
})) | ||
vi.mock("node:fs/promises", () => ({})) | ||
vi.mock("@inlang/telemetry", () => ({ | ||
parseOrigin: vi.fn().mockReturnValue("https://github.com/user/repo"), | ||
})) | ||
const nodeishFs = createNodeishMemoryFs() | ||
|
||
describe("getGitOrigin", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
// @ts-expect-error | ||
lixClient.findRepoRoot.mockResolvedValue("mock/root") | ||
// @ts-expect-error | ||
lixClient._listRemotes.mockResolvedValue([{ url: "git@github.com:user/repo.git" }]) | ||
const ciTestRepo: Snapshot = JSON.parse( | ||
readFileSync(resolve(__dirname, "../../../test/mocks/ci-test-repo.json"), { | ||
encoding: "utf-8", | ||
}) | ||
) | ||
|
||
loadSnapshot(nodeishFs, ciTestRepo) | ||
|
||
it("should return the parsed git origin URL", async () => { | ||
const origin = await getGitOrigin() | ||
expect(origin).toEqual("https://github.com/user/repo") | ||
describe("getGitOrigin", () => { | ||
it("should return the parsed git origin URL when workspace is present", async () => { | ||
const origin = await _getGitOrigin({ fs: nodeishFs, workspaceRoot: "src" }) | ||
expect(origin).toEqual("github.com/inlang/ci-test-repo.git") | ||
}) | ||
|
||
it("should handle errors and return undefined", async () => { | ||
// @ts-expect-error | ||
lixClient.findRepoRoot.mockRejectedValueOnce(new Error("Mock error")) | ||
const origin = await getGitOrigin() | ||
it("should handle errors and return undefined when no workspace folder is found", async () => { | ||
await nodeishFs.rm(".git", { recursive: true }) | ||
const origin = await _getGitOrigin({ fs: nodeishFs, workspaceRoot: "src" }) | ||
expect(origin).toBeUndefined() | ||
}) | ||
}) |
26 changes: 5 additions & 21 deletions
26
inlang/source-code/ide-extension/src/utilities/settings/getGitOrigin.ts
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 |
---|---|---|
@@ -1,29 +1,13 @@ | ||
/** | ||
* Gets the git origin url of the currently opened repository. | ||
* Gets the git origin url of the currently opened repository. Wrapper around _getGitOrigin.ts to allow testing with functional code | ||
*/ | ||
|
||
import { parseOrigin } from "@inlang/telemetry" | ||
import { findRepoRoot, _listRemotes } from "@lix-js/client" | ||
import { _getGitOrigin } from "./_getGitOrigin.js" | ||
|
||
import * as vscode from "vscode" | ||
import * as fs from "node:fs/promises" | ||
|
||
export async function getGitOrigin() { | ||
try { | ||
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath | ||
|
||
// FIXME _listRemotes is deprecated. openRepo and then call repo.listRemotes instead! | ||
const remotes = await _listRemotes({ | ||
fs, | ||
dir: ( | ||
await findRepoRoot({ | ||
nodeishFs: fs, | ||
path: workspaceRoot ?? process.cwd(), | ||
}) | ||
)?.replace("file://", ""), | ||
}) | ||
const gitOrigin = parseOrigin({ remotes }) | ||
return gitOrigin | ||
} catch (e) { | ||
return undefined | ||
} | ||
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? process.cwd() | ||
return _getGitOrigin({ fs, workspaceRoot }) | ||
} |
835 changes: 835 additions & 0 deletions
835
inlang/source-code/ide-extension/test/mocks/ci-test-repo.json
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
export { telemetryBrowser } from "./implementation/browser.js" | ||
export { telemetryNode } from "./implementation/node.js" | ||
|
||
export { parseOrigin } from "./utilities/parseOrigin.js" |
77 changes: 0 additions & 77 deletions
77
inlang/source-code/telemetry/src/utilities/parseOrigin.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.