Skip to content
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

Vscode test remove mocks #2750

Closed
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
5 changes: 5 additions & 0 deletions .changeset/moody-walls-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vs-code-extension": minor
---

Update openInEditor command title and add branch parameter
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest"
import { Uri, env } from "vscode"
import { openInEditorCommand } from "./openInEditor.js"
import { openInFinkCommand } from "./openInFink.js"
import { CONFIGURATION } from "../configuration.js"
import { getGitOrigin } from "../utilities/settings/getGitOrigin.js"

Expand All @@ -27,7 +27,7 @@ vi.mock("../utilities/settings/getGitOrigin.js", () => ({
getGitOrigin: vi.fn(),
}))

describe("openInEditorCommand", () => {
describe("openInFinkCommand", () => {
beforeEach(() => {
vi.clearAllMocks()

Expand All @@ -37,7 +37,7 @@ describe("openInEditorCommand", () => {
it("should open the editor with message id in URL", async () => {
const mockArgs = { messageId: "testMessageId", selectedProjectPath: "/test/path" }

await openInEditorCommand.callback(mockArgs)
await openInFinkCommand.callback(mockArgs)

expect(env.openExternal).toHaveBeenCalledWith(
Uri.parse(
Expand All @@ -54,7 +54,7 @@ describe("openInEditorCommand", () => {
const mockArgs = { messageId: "testMessageId", selectedProjectPath: "/test/path" }
vi.mocked(getGitOrigin).mockResolvedValue(undefined) // Simulate failure

await openInEditorCommand.callback(mockArgs)
await openInFinkCommand.callback(mockArgs)

expect(env.openExternal).toHaveBeenCalledWith(
Uri.parse(`${CONFIGURATION.STRINGS.EDITOR_BASE_URL}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import { telemetry } from "../services/telemetry/implementation.js"
import type { Message } from "@inlang/sdk"
import { CONFIGURATION } from "../configuration.js"
import { getGitOrigin } from "../utilities/settings/getGitOrigin.js"
import { getCurrentBranch } from "../utilities/settings/getCurrentBranch.js"

export const openInEditorCommand = {
command: "sherlock.openInEditor",
title: "Sherlock: Open in Editor",
export const openInFinkCommand = {
command: "sherlock.openInFink",
title: "Sherlock: Open in Fink",
register: commands.registerCommand,
callback: async function (args: { messageId: Message["id"]; selectedProjectPath: string }) {
// TODO: Probably the origin should be configurable via the config.
const origin = (await getGitOrigin())?.replaceAll(".git", "")
const branch = (await getCurrentBranch()) || "main"
const uri = args.messageId
? `${CONFIGURATION.STRINGS.EDITOR_BASE_URL}${origin}?project=${encodeURIComponent(
args.selectedProjectPath
)}&id=${encodeURIComponent(args.messageId)}`
: `${CONFIGURATION.STRINGS.EDITOR_BASE_URL}${origin}`
)}&branch=${encodeURIComponent(branch)}&id=${encodeURIComponent(args.messageId)}`
: `${CONFIGURATION.STRINGS.EDITOR_BASE_URL}${origin}?branch=${encodeURIComponent(branch)}`

env.openExternal(Uri.parse(uri))

Expand Down
4 changes: 2 additions & 2 deletions inlang/source-code/ide-extension/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openInEditorCommand } from "./commands/openInEditor.js"
import { openInFinkCommand } from "./commands/openInFink.js"
import { extractMessageCommand } from "./commands/extractMessage.js"
import { editMessageCommand } from "./commands/editMessage.js"
import { EventEmitter } from "vscode"
Expand Down Expand Up @@ -27,7 +27,7 @@ export const CONFIGURATION = {
EXTRACT_MESSAGE: extractMessageCommand,
SET_PREVIEW_LANGUAGETAG: previewLanguageTagCommand,
JUMP_TO_POSITION: jumpToPositionCommand,
OPEN_IN_EDITOR: openInEditorCommand,
OPEN_IN_EDITOR: openInFinkCommand,
OPEN_PROJECT: openProjectCommand,
OPEN_SETTINGS_FILE: openSettingsFileCommand,
OPEN_SETTINGS_VIEW: openSettingsViewCommand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ type ContextTableRow = {
language: LanguageTag
message: string
editCommand?: Uri
openInEditorCommand?: Uri
openInFinkCommand?: Uri
}

function renderTranslationRow(row: ContextTableRow) {
const editCommandLink = row.editCommand ? `<a href="${row.editCommand}">$(edit)</a>` : ""
const openInEditorLink = row.openInEditorCommand
? `<a href="${row.openInEditorCommand}">$(link-external)</a>`
const openInFinkLink = row.openInFinkCommand
? `<a href="${row.openInFinkCommand}">$(link-external)</a>`
: ""
const messageListing = `<td><strong>${escapeHtml(
row.language
)}&nbsp;</strong></td><td>${escapeHtml(row.message)}</td>`
const editCommandCell = editCommandLink ? `<td>&nbsp;&nbsp;${editCommandLink}</td>` : ""
const openInEditorCell = openInEditorLink ? `<td>&nbsp;${openInEditorLink}</td>` : ""
return `<tr>${messageListing}${editCommandCell}${openInEditorCell}</tr>`
const openInFinkCell = openInFinkLink ? `<td>&nbsp;${openInFinkLink}</td>` : ""
return `<tr>${messageListing}${editCommandCell}${openInFinkCell}</tr>`
}

export function contextTooltip(
Expand Down Expand Up @@ -64,13 +64,13 @@ export function contextTooltip(
)

const editCommand = Uri.parse(INTERPOLATE.COMMAND_URI("EDIT_MESSAGE", args))
const openInEditorCommand = Uri.parse(INTERPOLATE.COMMAND_URI("OPEN_IN_EDITOR", args))
const openInFinkCommand = Uri.parse(INTERPOLATE.COMMAND_URI("OPEN_IN_EDITOR", args))

return {
language: languageTag,
message: m,
editCommand,
openInEditorCommand,
openInFinkCommand,
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export function createMessageHtml(args: {

const positionHtml = encodeURIComponent(JSON.stringify(args.position))
const jumpCommand = `jumpToPosition('${args.message.id}', '${positionHtml}');event.stopPropagation();`
const openCommand = `openInEditor('${args.message.id}', '${relativeProjectPathFromWorkspace}');event.stopPropagation();`
const openCommand = `openInFink('${args.message.id}', '${relativeProjectPathFromWorkspace}');event.stopPropagation();`

return `
<div class="tree-item">
Expand Down Expand Up @@ -446,10 +446,10 @@ export function getHtml(args: {
});
}

function openInEditor(messageId, selectedProjectPath) {
function openInFink(messageId, selectedProjectPath) {
vscode.postMessage({
command: 'executeCommand',
commandName: 'sherlock.openInEditor',
commandName: 'sherlock.openInFink',
commandArgs: { messageId, selectedProjectPath },
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Gets current git branch of the currently opened repository.
*/

import { findRepoRoot, openRepository } from "@lix-js/client"
import type { NodeishFilesystem } from "@lix-js/fs"

export async function _getCurrentBranch({
fs,
workspaceRoot,
}: {
fs: NodeishFilesystem
workspaceRoot: string | undefined
}): Promise<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.getCurrentBranch()
} catch (e) {
return undefined
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, it, expect, vi } from "vitest"
import { createNodeishMemoryFs, fromSnapshot as loadSnapshot, type Snapshot } from "@lix-js/fs"
import { readFileSync } from "node:fs"
import { resolve } from "node:path"
import { _getCurrentBranch } from "./_getCurrentBranch.js"
import { findRepoRoot } from "@lix-js/client"

const nodeishFs = createNodeishMemoryFs()

const ciTestRepo: Snapshot = JSON.parse(
readFileSync(resolve(__dirname, "../../../test/mocks/ci-test-repo.json"), {
encoding: "utf-8",
})
)

loadSnapshot(nodeishFs, ciTestRepo)

describe("getCurrentBranch", () => {
it("should return the current git branch when the repository root is found", async () => {
const branch = await _getCurrentBranch({ fs: nodeishFs, workspaceRoot: "src" })
expect(branch).toEqual("test-symlink")
})

it("should return undefined when no repository root is found", async () => {
// Correctly handle the case where the repository root is not found
await nodeishFs.rm(".git", { recursive: true })
const branch = await _getCurrentBranch({ fs: nodeishFs, workspaceRoot: "src" })
expect(branch).toBeUndefined()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Get the current branch of the git repository. Wrapper around _getGitBranch.ts to allow testing with functional code
*/

import { _getCurrentBranch } from "./_getCurrentBranch.js"

import * as vscode from "vscode"
import * as fs from "node:fs/promises"

export async function getCurrentBranch() {
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? process.cwd()
return _getCurrentBranch({ fs, workspaceRoot })
}
Loading