Skip to content

Commit

Permalink
Correct showPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Oct 31, 2020
1 parent 8be345a commit 0f923ad
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,15 +857,31 @@ export class GitExtension implements IGitExtension {
* @throws {Git.GitResponseError} If the server response is not ok
* @throws {ServerConnection.NetworkError} If the request cannot be made
*/
async showPrefix(path: string): Promise<Git.IShowPrefixResult> {
return await this._taskHandler.execute<Git.IShowPrefixResult>(
'git:fetch:prefix_path',
async () => {
return await requestAPI<Git.IShowPrefixResult>('show_prefix', 'POST', {
current_path: path
});
async showPrefix(path: string): Promise<string | null> {
try {
const data = await this._taskHandler.execute<Git.IShowPrefixResult>(
'git:fetch:prefix_path',
async () => {
return await requestAPI<Git.IShowPrefixResult>(
'show_prefix',
'POST',
{
current_path: path
}
);
}
);
return data.under_repo_path || null;
} catch (error) {
if (
error instanceof Git.GitResponseError &&
error.response.status === 500 &&
error.json.code === 128
) {
return null;
}
);
throw error;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export interface IGitExtension extends IDisposable {
* @throws {Git.GitResponseError} If the server response is not ok
* @throws {ServerConnection.NetworkError} If the request cannot be made
*/
showPrefix(path: string): Promise<Git.IShowPrefixResult>;
showPrefix(path: string): Promise<string | null>;

/**
* Get the top level path of repository 'path'
Expand Down
38 changes: 38 additions & 0 deletions tests/model.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,44 @@ describe('IGitExtension', () => {
});
});

describe('#showPrefix', () => {
it('should return a string if the folder is a git repository', async () => {
const fakeRepo = '/repo';
mockResponses['show_prefix'] = {
body: () => {
return { code: 0, under_repo_path: fakeRepo };
}
};
const topLevel = await model.showPrefix('/repo/cwd');
expect(topLevel).toEqual(fakeRepo);
});

it('should return null if the repository is not a git repository', async () => {
mockResponses['show_prefix'] = {
body: () => {
return { code: 128 };
},
status: 500
};
const topLevel = await model.showPrefix('/repo/cwd');
expect(topLevel).toBeNull();
});

it('should throw an exception if the server otherwise', async () => {
mockResponses['show_prefix'] = {
body: () => {
return { code: 128 };
},
status: 401
};
try {
await model.showPrefix('/repo/cwd');
} catch (error) {
expect(error).toBeInstanceOf(Git.GitResponseError);
}
});
});

describe('#showTopLevel', () => {
it('should return a string if the folder is a git repository', async () => {
const fakeRepo = '/path/to/repo';
Expand Down

0 comments on commit 0f923ad

Please sign in to comment.