|
1 | 1 | const proxyquire = require('proxyquire') |
2 | 2 |
|
3 | 3 | const sanitizedExecStub = sinon.stub() |
| 4 | +const gitRepoInfoStub = sinon.stub().returns({ |
| 5 | + author: 'author <author@commit.com>', |
| 6 | + committer: 'committer <committer@commit.com>', |
| 7 | + authorDate: '1970', |
| 8 | + committerDate: '1971', |
| 9 | + commitMessage: 'commit message', |
| 10 | + branch: 'gitBranch', |
| 11 | + tag: 'gitTag', |
| 12 | + sha: 'gitSha' |
| 13 | +}) |
| 14 | + |
4 | 15 | const { |
5 | 16 | getGitMetadata, |
6 | 17 | GIT_COMMIT_SHA, |
7 | 18 | GIT_BRANCH, |
8 | | - GIT_REPOSITORY_URL |
9 | | -} = proxyquire('../../../src/plugins/util/git', { './exec': { |
10 | | - 'sanitizedExec': sanitizedExecStub |
11 | | -} }) |
| 19 | + GIT_TAG, |
| 20 | + GIT_REPOSITORY_URL, |
| 21 | + GIT_COMMIT_MESSAGE, |
| 22 | + GIT_COMMIT_COMMITTER_DATE, |
| 23 | + GIT_COMMIT_COMMITTER_EMAIL, |
| 24 | + GIT_COMMIT_COMMITTER_NAME, |
| 25 | + GIT_COMMIT_AUTHOR_DATE, |
| 26 | + GIT_COMMIT_AUTHOR_EMAIL, |
| 27 | + GIT_COMMIT_AUTHOR_NAME |
| 28 | +} = proxyquire('../../../src/plugins/util/git', |
| 29 | + { |
| 30 | + './exec': { |
| 31 | + 'sanitizedExec': sanitizedExecStub |
| 32 | + }, |
| 33 | + '../../../../../vendor/git-repo-info': gitRepoInfoStub |
| 34 | + } |
| 35 | +) |
12 | 36 |
|
13 | 37 | describe('git', () => { |
14 | 38 | afterEach(() => { |
15 | 39 | sanitizedExecStub.reset() |
16 | 40 | }) |
| 41 | + const commonGitMetadata = { |
| 42 | + [GIT_COMMIT_MESSAGE]: 'commit message', |
| 43 | + [GIT_COMMIT_COMMITTER_DATE]: '1971', |
| 44 | + [GIT_COMMIT_COMMITTER_EMAIL]: 'committer@commit.com', |
| 45 | + [GIT_COMMIT_COMMITTER_NAME]: 'committer', |
| 46 | + [GIT_COMMIT_AUTHOR_DATE]: '1970', |
| 47 | + [GIT_COMMIT_AUTHOR_EMAIL]: 'author@commit.com', |
| 48 | + [GIT_COMMIT_AUTHOR_NAME]: 'author', |
| 49 | + [GIT_TAG]: 'gitTag', |
| 50 | + [GIT_BRANCH]: 'gitBranch' |
| 51 | + } |
17 | 52 | it('calls git when some ci metadata is not present', () => { |
18 | | - const ciMetadata = { commitSHA: 'ciSHA', branch: 'ciBranch' } |
| 53 | + const ciMetadata = { commitSHA: 'ciSHA' } |
19 | 54 | const metadata = getGitMetadata(ciMetadata) |
20 | 55 |
|
21 | 56 | expect(metadata).to.include( |
22 | 57 | { |
23 | 58 | [GIT_COMMIT_SHA]: 'ciSHA', |
24 | | - [GIT_BRANCH]: 'ciBranch' |
| 59 | + ...commonGitMetadata |
25 | 60 | } |
26 | 61 | ) |
27 | 62 | expect(metadata[GIT_REPOSITORY_URL]).not.to.equal('ciRepositoryUrl') |
28 | 63 | expect(sanitizedExecStub).to.have.been.calledWith('git ls-remote --get-url', { stdio: 'pipe' }) |
| 64 | + expect(gitRepoInfoStub).to.have.been.called |
29 | 65 | }) |
30 | 66 | it('returns ci metadata if present and does not call git', () => { |
31 | | - const ciMetadata = { commitSHA: 'ciSHA', branch: 'ciBranch', repositoryUrl: 'ciRepositoryUrl' } |
| 67 | + const ciMetadata = { commitSHA: 'ciSHA', branch: 'ciBranch', repositoryUrl: 'ciRepositoryUrl', tag: 'tag' } |
32 | 68 | const metadata = getGitMetadata(ciMetadata) |
33 | 69 |
|
34 | 70 | expect(metadata).to.eql( |
35 | | - { [GIT_COMMIT_SHA]: 'ciSHA', [GIT_BRANCH]: 'ciBranch', [GIT_REPOSITORY_URL]: 'ciRepositoryUrl' } |
| 71 | + { |
| 72 | + ...commonGitMetadata, |
| 73 | + [GIT_COMMIT_SHA]: 'ciSHA', |
| 74 | + [GIT_BRANCH]: 'ciBranch', |
| 75 | + [GIT_REPOSITORY_URL]: 'ciRepositoryUrl', |
| 76 | + [GIT_TAG]: 'tag' |
| 77 | + } |
36 | 78 | ) |
37 | 79 | expect(sanitizedExecStub).not.to.have.been.called |
38 | 80 | }) |
| 81 | + it('does not crash with empty or badly shapen author or committer', () => { |
| 82 | + gitRepoInfoStub.returns({ |
| 83 | + author: 'author <>', |
| 84 | + committer: undefined, |
| 85 | + authorDate: '1970', |
| 86 | + committerDate: '1971', |
| 87 | + commitMessage: 'commit message', |
| 88 | + branch: 'gitBranch', |
| 89 | + tag: 'gitTag', |
| 90 | + sha: 'gitSha' |
| 91 | + }) |
| 92 | + const ciMetadata = { repositoryUrl: 'ciRepositoryUrl' } |
| 93 | + const metadata = getGitMetadata(ciMetadata) |
| 94 | + |
| 95 | + expect(metadata).to.eql( |
| 96 | + { |
| 97 | + [GIT_COMMIT_MESSAGE]: 'commit message', |
| 98 | + [GIT_COMMIT_COMMITTER_DATE]: '1971', |
| 99 | + [GIT_COMMIT_COMMITTER_EMAIL]: '', |
| 100 | + [GIT_COMMIT_COMMITTER_NAME]: '', |
| 101 | + [GIT_COMMIT_AUTHOR_DATE]: '1970', |
| 102 | + [GIT_COMMIT_AUTHOR_EMAIL]: '', |
| 103 | + [GIT_COMMIT_AUTHOR_NAME]: 'author', |
| 104 | + [GIT_TAG]: 'gitTag', |
| 105 | + [GIT_BRANCH]: 'gitBranch', |
| 106 | + [GIT_COMMIT_SHA]: 'gitSha', |
| 107 | + [GIT_REPOSITORY_URL]: 'ciRepositoryUrl' |
| 108 | + } |
| 109 | + ) |
| 110 | + }) |
39 | 111 | }) |
0 commit comments