Skip to content

Commit

Permalink
feat: use LogOutputChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed Jun 4, 2024
1 parent 7bee741 commit 77b2aae
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
Disposable,
window,
workspace,
OutputChannel,
SourceControlResourceState,
SourceControlResourceGroup,
TextDocumentShowOptions,
ViewColumn,
Selection,
ExtensionContext,
SourceControl,
LogOutputChannel,
} from 'vscode';
import { LineChange, revertChanges } from './revert';
import * as path from 'path';
Expand Down Expand Up @@ -182,7 +182,7 @@ export class CommandCenter {
constructor(
private readonly executable: FossilExecutable,
private readonly model: Model,
private readonly outputChannel: OutputChannel,
private readonly outputChannel: LogOutputChannel,
context: ExtensionContext
) {
this.previewManager = new FossilPreviewManager(context, executable);
Expand Down Expand Up @@ -1398,7 +1398,7 @@ export class CommandCenter {
}
}
} else {
this.outputChannel.appendLine(
this.outputChannel.error(
"couldn't create wiki entity - no active preview"
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/fossilExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from './openedRepository';
import * as path from 'path';
import * as fs from 'fs/promises';
import { window, OutputChannel, ProgressLocation } from 'vscode';
import { window, LogOutputChannel, ProgressLocation } from 'vscode';
import * as cp from 'child_process';
import { dispose, IDisposable, toDisposable } from './util';
import * as interaction from './interaction';
Expand Down Expand Up @@ -129,7 +129,7 @@ export function toString(this: ExecFailure): string {
export class FossilExecutable {
private fossilPath!: FossilExecutablePath;
public version!: FossilVersion;
constructor(private readonly outputChannel: OutputChannel) {}
constructor(public readonly outputChannel: LogOutputChannel) {}

setInfo(info: FossilExecutableInfo) {
this.fossilPath = info.path;
Expand Down Expand Up @@ -362,7 +362,7 @@ export class FossilExecutable {
})();

if (options.logErrors !== false && result.stderr) {
this.log(`${result.stderr}\n`);
this.outputChannel.error(result.stderr);
}
const failure: ExecFailure = {
...result,
Expand All @@ -385,8 +385,8 @@ export class FossilExecutable {
return result as ExecSuccess;
}

public log(output: string): void {
this.outputChannel.appendLine(output);
private log(output: string): void {
this.outputChannel.info(output);
}
private logArgs(args: FossilArgs, reason: string, info: string): void {
if (args[0] == 'clone') {
Expand Down
21 changes: 13 additions & 8 deletions src/fossilFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
FossilVersion,
} from './fossilExecutable';
import { localize } from './main';
import { LogOutputChannel } from 'vscode';

export type UnvalidatedFossilExecutablePath = Distinct<
string,
Expand Down Expand Up @@ -38,7 +39,7 @@ function getVersion(

export async function findFossil(
hint: UnvalidatedFossilExecutablePath,
logLine: (test: string) => void
outputChannel: LogOutputChannel
): Promise<FossilExecutableInfo | undefined> {
for (const [path, isHint] of [
[hint, 1],
Expand All @@ -49,11 +50,15 @@ export async function findFossil(
try {
stdout = await getVersion(path);
} catch (e: unknown) {
logLine(
isHint
? `\`fossil.path\` '${path}' is unavailable (${e}). Will try 'fossil' as the path`
: `'${path}' is unavailable (${e}). Fossil extension commands will be disabled`
);
if (isHint) {
outputChannel.warn(
`\`fossil.path\` '${path}' is unavailable (${e}). Will try 'fossil' as the path`
);
} else {
outputChannel.error(
`'${path}' is unavailable (${e}). Fossil extension commands will be disabled`
);
}
continue;
}

Expand All @@ -64,12 +69,12 @@ export async function findFossil(
.split('.')
.map(s => parseInt(s)) as FossilVersion;
} else {
logLine(
outputChannel.error(
`Failed to parse fossil version from output: '${stdout}'`
);
}

logLine(
outputChannel.info(
localize(
'using fossil',
'Using fossil {0} from {1}',
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ async function init(context: ExtensionContext): Promise<Model | undefined> {
new Disposable(() => Disposable.from(...disposables).dispose())
);

const outputChannel = window.createOutputChannel('Fossil');
const outputChannel = window.createOutputChannel('Fossil', { log: true });
disposables.push(outputChannel);

const fossilHist = typedConfig.path;
const fossilInfo = await findFossil(fossilHist, outputChannel.appendLine);
const fossilInfo = await findFossil(fossilHist, outputChannel);
const executable = new FossilExecutable(outputChannel);

const model = new Model(executable, fossilHist);
Expand Down
7 changes: 3 additions & 4 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,9 @@ export class Model implements Disposable {
const fossilHint = typedConfig.path;
if (this.lastUsedHist != fossilHint) {
this.lastUsedHist = fossilHint;
return findFossil(
fossilHint,
this.executable.log.bind(this.executable)
).then(this.foundExecutable.bind(this));
return findFossil(fossilHint, this.executable.outputChannel).then(
this.foundExecutable.bind(this)
);
}
}

Expand Down
29 changes: 19 additions & 10 deletions src/test/suite/setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { window, Uri, workspace, ExtensionContext, commands } from 'vscode';
import {
window,
Uri,
workspace,
ExtensionContext,
commands,
LogOutputChannel,
} from 'vscode';
import {
SinonStubT,
cleanRoot,
Expand Down Expand Up @@ -35,26 +42,28 @@ function ExecutableSuite(this: Suite): void {
after(resetIgnoreMissingFossilWarning);

test('Invalid executable path', async () => {
const appendLine = sinon.stub();
const outputChanel = {
info: sinon.stub(),
warn: sinon.stub(),
};
await fossilFinder.findFossil(
'non_existing_fossil' as UnvalidatedFossilExecutablePath,
appendLine
outputChanel as unknown as LogOutputChannel
);
sinon.assert.calledTwice(appendLine);
sinon.assert.calledWithExactly(
appendLine.firstCall,
sinon.assert.calledOnceWithExactly(
outputChanel.warn,
"`fossil.path` 'non_existing_fossil' is unavailable " +
'(Error: spawn non_existing_fossil ENOENT). ' +
"Will try 'fossil' as the path"
);
sinon.assert.calledWithMatch(
appendLine.secondCall,
outputChanel.info,
/^Using fossil \d.\d+ from fossil$/
);
}).timeout(2000);

test('Execution error is caught and shown', async () => {
const appendLine = sinon.stub();
const outputChanel = { error: sinon.stub() };
const childProcess = {
on: sinon
.stub()
Expand All @@ -73,10 +82,10 @@ function ExecutableSuite(this: Suite): void {
);
await fossilFinder.findFossil(
'' as UnvalidatedFossilExecutablePath,
appendLine
outputChanel as unknown as LogOutputChannel
);
sinon.assert.calledOnceWithExactly(
appendLine,
outputChanel.error,
"'fossil' is unavailable (Error: mocked error). " +
'Fossil extension commands will be disabled'
);
Expand Down

0 comments on commit 77b2aae

Please sign in to comment.