|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { default as assert } from 'assert'; |
| 7 | +import { parseRepositoryRemotesAsync } from '../../common/remote'; |
| 8 | +import { MockRepository } from '../mocks/mockRepository'; |
| 9 | + |
| 10 | +describe('parseRepositoryRemotesAsync', () => { |
| 11 | + it('resolves a remote URL using a global "url.<base>.insteadOf" alias', async () => { |
| 12 | + const repository = new MockRepository(); |
| 13 | + // Simulate `git config url."https://github.abc.com/".insteadOf github:` |
| 14 | + await repository.setConfig('url.https://github.abc.com/.insteadof', 'github:'); |
| 15 | + // And a remote stored as `github:org/repo` (as returned by `git config --get remote.origin.url` |
| 16 | + // when the user clones with `git clone github:org/repo`). |
| 17 | + await repository.addRemote('origin', 'github:org/repo'); |
| 18 | + |
| 19 | + const remotes = await parseRepositoryRemotesAsync(repository); |
| 20 | + |
| 21 | + assert.strictEqual(remotes.length, 1); |
| 22 | + assert.strictEqual(remotes[0].remoteName, 'origin'); |
| 23 | + assert.strictEqual(remotes[0].host, 'github.abc.com'); |
| 24 | + assert.strictEqual(remotes[0].owner, 'org'); |
| 25 | + assert.strictEqual(remotes[0].repositoryName, 'repo'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('applies the longest matching insteadOf prefix when multiple match', async () => { |
| 29 | + const repository = new MockRepository(); |
| 30 | + await repository.setConfig('url.https://short.example.com/.insteadof', 'gh:'); |
| 31 | + await repository.setConfig('url.https://long.example.com/.insteadof', 'gh:org/'); |
| 32 | + await repository.addRemote('origin', 'gh:org/repo'); |
| 33 | + |
| 34 | + const remotes = await parseRepositoryRemotesAsync(repository); |
| 35 | + |
| 36 | + assert.strictEqual(remotes.length, 1); |
| 37 | + assert.strictEqual(remotes[0].host, 'long.example.com'); |
| 38 | + assert.strictEqual(remotes[0].repositoryName, 'repo'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('leaves URLs unchanged when no insteadOf alias matches', async () => { |
| 42 | + const repository = new MockRepository(); |
| 43 | + await repository.setConfig('url.https://github.abc.com/.insteadof', 'github:'); |
| 44 | + await repository.addRemote('origin', 'https://github.com/owner/repo'); |
| 45 | + |
| 46 | + const remotes = await parseRepositoryRemotesAsync(repository); |
| 47 | + |
| 48 | + assert.strictEqual(remotes.length, 1); |
| 49 | + assert.strictEqual(remotes[0].host, 'github.com'); |
| 50 | + assert.strictEqual(remotes[0].owner, 'owner'); |
| 51 | + assert.strictEqual(remotes[0].repositoryName, 'repo'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments