Skip to content
Merged
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
71 changes: 71 additions & 0 deletions src/test/view/gitContentProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { default as assert } from 'assert';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import * as vscode from 'vscode';
import { GitApiImpl } from '../../api/api1';
import { toReviewUri } from '../../common/uri';
import { CredentialStore } from '../../github/credentials';
import { GitContentFileSystemProvider } from '../../view/gitContentProvider';
import { MockRepository } from '../mocks/mockRepository';

describe('GitContentFileSystemProvider', function () {
let sandbox: SinonSandbox;
let provider: GitContentFileSystemProvider;
let showErrorMessage: SinonStub;

beforeEach(function () {
sandbox = createSandbox();
const gitApi = {
state: 'initialized',
repositories: [new MockRepository()],
} as unknown as GitApiImpl;
const credentialStore = {
isAnyAuthenticated: () => true,
} as unknown as CredentialStore;
provider = new GitContentFileSystemProvider(gitApi, credentialStore, () => []);
provider.registerTextDocumentContentFallback(async () => '');
showErrorMessage = sandbox.stub(vscode.window, 'showErrorMessage').resolves(undefined);
});

afterEach(function () {
sandbox.restore();
});

it('does not show an error for a repository from a previous workspace', async function () {
sandbox.stub(vscode.workspace, 'getWorkspaceFolder').returns(undefined);

await provider.readFile(createReviewUri('/previous-workspace'));

assert.strictEqual(showErrorMessage.called, false);
});

it('shows an error when the repository belongs to the current workspace', async function () {
const rootUri = vscode.Uri.file('/current-workspace');
sandbox.stub(vscode.workspace, 'getWorkspaceFolder').returns({
uri: rootUri,
name: 'current-workspace',
index: 0,
});

await provider.readFile(createReviewUri(rootUri.path));

assert.strictEqual(showErrorMessage.calledOnce, true);
});
});

function createReviewUri(rootPath: string): vscode.Uri {
const rootUri = vscode.Uri.file(rootPath);
return toReviewUri(
vscode.Uri.joinPath(rootUri, 'file.ts'),
'file.ts',
undefined,
'commit',
false,
{ base: true },
rootUri,
);
}
6 changes: 5 additions & 1 deletion src/view/gitContentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class GitContentFileSystemProvider extends RepositoryFileSystemProvider {

const repository = await this.getRepositoryForFile(vscode.Uri.file(rootPath));
if (!repository) {
vscode.window.showErrorMessage(`We couldn't find an open repository for ${commit} locally.`);
if (vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootPath))) {
vscode.window.showErrorMessage(`We couldn't find an open repository for ${commit} locally.`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point?

} else {
Logger.debug(`Skipping content for commit ${commit} because ${rootPath} is no longer in the workspace.`, GitContentFileSystemProvider.ID);
}
return new TextEncoder().encode('');
}

Expand Down