File: git.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (251 lines) | stat: -rw-r--r-- 9,204 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* @flow */

jest.mock('../../src/util/git/git-spawn.js', () => ({
  spawn: jest.fn(([command]) => {
    switch (command) {
      case 'ls-remote':
        return `Identity added: /Users/example/.ssh/id_dsa (/Users/example/.ssh/id_dsa)
ref: refs/heads/master  HEAD
7a053e2ca07d19b2e2eebeeb0c27edaacfd67904        HEAD`;
      case 'rev-list':
        return Promise.resolve('7a053e2ca07d19b2e2eebeeb0c27edaacfd67904 Fix ...');
      case 'show-ref':
        return `7a053e2ca07d19b2e2eebeeb0c27edaacfd67904 refs/remotes/origin/HEAD`;
    }
    return Promise.resolve('');
  }),
}));

import Config from '../../src/config.js';
import Git from '../../src/util/git.js';
import {spawn as spawnGit} from '../../src/util/git/git-spawn.js';
import {NoopReporter, BufferReporter} from '../../src/reporters/index.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;

test('npmUrlToGitUrl', () => {
  expect(Git.npmUrlToGitUrl('git+https://github.com/npm-opam/ocamlfind.git')).toEqual({
    protocol: 'https:',
    hostname: 'github.com',
    repository: 'https://github.com/npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('https://github.com/npm-opam/ocamlfind.git')).toEqual({
    protocol: 'https:',
    hostname: 'github.com',
    repository: 'https://github.com/npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('git://github.com/npm-opam/ocamlfind.git')).toEqual({
    protocol: 'git:',
    hostname: 'github.com',
    repository: 'git://github.com/npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('git+ssh://git@gitlab.mydomain.tld:10202/project-name/my-package.git')).toEqual({
    protocol: 'ssh:',
    hostname: 'gitlab.mydomain.tld',
    repository: 'ssh://git@gitlab.mydomain.tld:10202/project-name/my-package.git',
  });
  expect(Git.npmUrlToGitUrl('git+ssh://git@github.com/npm-opam/ocamlfind.git')).toEqual({
    protocol: 'ssh:',
    hostname: 'github.com',
    repository: 'ssh://git@github.com/npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('git+ssh://scp-host-nickname:npm-opam/ocamlfind.git')).toEqual({
    protocol: 'ssh:',
    hostname: 'scp-host-nickname',
    repository: 'scp-host-nickname:npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('git+ssh://user@scp-host-nickname:npm-opam/ocamlfind.git')).toEqual({
    protocol: 'ssh:',
    hostname: 'scp-host-nickname',
    repository: 'user@scp-host-nickname:npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('github:npm-opam/ocamlfind.git#v1.2.3')).toEqual({
    protocol: 'ssh:',
    hostname: 'github.com',
    repository: 'ssh://git@github.com/npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('github:npm-opam/ocamlfind#v1.2.3')).toEqual({
    protocol: 'ssh:',
    hostname: 'github.com',
    repository: 'ssh://git@github.com/npm-opam/ocamlfind',
  });
  expect(Git.npmUrlToGitUrl('github:npm-opam/ocamlfind.git')).toEqual({
    protocol: 'ssh:',
    hostname: 'github.com',
    repository: 'ssh://git@github.com/npm-opam/ocamlfind.git',
  });
  expect(Git.npmUrlToGitUrl('github:npm-opam/ocamlfind')).toEqual({
    protocol: 'ssh:',
    hostname: 'github.com',
    repository: 'ssh://git@github.com/npm-opam/ocamlfind',
  });
  expect(Git.npmUrlToGitUrl('git+file:../ocalmfind.git')).toEqual({
    protocol: 'file:',
    hostname: null,
    repository: '../ocalmfind.git',
  });
  expect(Git.npmUrlToGitUrl('git+file:../ocalmfind')).toEqual({
    protocol: 'file:',
    hostname: null,
    repository: '../ocalmfind',
  });
});

describe('secureGitUrl()', function() {
  it('accepts URLs with commit hash', async () => {
    // "accept" means it returns the Url unchanged and doesn't report a warning
    // Assume the all the repos exist
    const mock = jest.spyOn(Git, 'repoExists').mockImplementation(() => true);

    // Test it for all the supported protocols
    for (const protocol of ['http:', 'https:', 'git:', 'ssh:']) {
      const inputurl = protocol + '//example.com/repo.git';
      const hash = '0123456789abcdef0123456789abcdef';
      const reporter = new BufferReporter();
      const secureUrl = await Git.secureGitUrl(Git.npmUrlToGitUrl(inputurl), hash, reporter);
      expect(secureUrl.repository).toEqual(inputurl);
      expect(reporter.getBuffer()).toEqual([]);
    }

    mock.mockRestore();
  });

  it('accepts URLs without commit hash over secure protocols', async () => {
    const mock = jest.spyOn(Git, 'repoExists').mockImplementation(() => true);
    for (const protocol of ['https:', 'ssh:']) {
      const inputurl = protocol + '//example.com/repo.git';
      const reporter = new BufferReporter();
      const secureUrl = await Git.secureGitUrl(Git.npmUrlToGitUrl(inputurl), '', reporter);
      expect(secureUrl.repository).toEqual(inputurl);
      expect(reporter.getBuffer()).toEqual([]);
    }
    mock.mockRestore();
  });

  it('changes insecure URLs to HTTPS', async () => {
    // Assume the HTTPS URLs exist
    const mock = jest.spyOn(Git, 'repoExists').mockImplementation(() => true);
    for (const protocol of ['http:', 'git:']) {
      const inputurl = protocol + '//example.com/repo.git';
      const reporter = new BufferReporter();
      const secureUrl = await Git.secureGitUrl(Git.npmUrlToGitUrl(inputurl), '', reporter);
      expect(secureUrl.repository).toEqual('https://example.com/repo.git');
      expect(reporter.getBuffer()).toEqual([]);
    }
    mock.mockRestore();
  });

  it('warns if insecure URLs cannot be changed to HTTPS', async () => {
    // Assume the HTTPS URLs don't exist
    const mock = jest.spyOn(Git, 'repoExists').mockImplementation(() => false);
    for (const protocol of ['http:', 'git:']) {
      const inputurl = protocol + '//example.com/repo.git';
      const reporter = new BufferReporter();
      const secureUrl = await Git.secureGitUrl(Git.npmUrlToGitUrl(inputurl), '', reporter);
      expect(secureUrl.repository).toEqual(inputurl);
      expect(reporter.getBuffer()).toEqual([
        {
          type: 'warning',
          error: true,
          data: reporter.lang(protocol == 'git:' ? 'downloadGitWithoutCommit' : 'downloadHTTPWithoutCommit', inputurl),
        },
      ]);
    }
    mock.mockRestore();
  });

  // In case some test case didn't clean up correctly, for example if it failed with an exception
  afterAll(() => {
    // restoreAllMocks() is included in jest >= 21.1.0, but only known in flow-typed for jest >= 22.0.0
    (jest: any).restoreAllMocks();
  });
});

test('secureGitUrl', async function(): Promise<void> {
  const reporter = new NoopReporter();

  const originalRepoExists = Git.repoExists;
  (Git: any).repoExists = jest.fn();
  Git.repoExists.mockImplementation(() => Promise.resolve(true)).mockImplementationOnce(() => {
    throw new Error('Non-existent repo!');
  });

  let hasException = false;
  try {
    await Git.secureGitUrl(Git.npmUrlToGitUrl('http://fake-fake-fake-fake.com/123.git'), '', reporter);
  } catch (e) {
    hasException = true;
  }
  (Git: any).repoExists = originalRepoExists;
  expect(hasException).toEqual(true);

  let gitURL = await Git.secureGitUrl(Git.npmUrlToGitUrl('http://github.com/yarnpkg/yarn.git'), '', reporter);
  expect(gitURL.repository).toEqual('https://github.com/yarnpkg/yarn.git');

  gitURL = await Git.secureGitUrl(Git.npmUrlToGitUrl('https://github.com/yarnpkg/yarn.git'), '', reporter);
  expect(gitURL.repository).toEqual('https://github.com/yarnpkg/yarn.git');

  gitURL = await Git.secureGitUrl(Git.npmUrlToGitUrl('git://github.com/yarnpkg/yarn.git'), '', reporter);
  expect(gitURL.repository).toEqual('https://github.com/yarnpkg/yarn.git');
});

test('resolveDefaultBranch when local', async () => {
  const spawnGitMock = (spawnGit: any).mock;
  const config = await Config.create();
  const git = new Git(
    config,
    {
      protocol: 'file:',
      hostname: undefined,
      repository: 'example',
    },
    '',
  );
  expect(await git.resolveDefaultBranch()).toEqual({
    sha: '7a053e2ca07d19b2e2eebeeb0c27edaacfd67904',
    ref: undefined,
  });
  const lastCall = spawnGitMock.calls[spawnGitMock.calls.length - 1];
  expect(lastCall[0]).toContain('show-ref');
});

test('resolveDefaultBranch when remote', async () => {
  const spawnGitMock = (spawnGit: any).mock;
  const config = await Config.create();
  const git = new Git(
    config,
    {
      protocol: 'https:',
      hostname: '//example.com',
      repository: 'example',
    },
    '',
  );
  expect(await git.resolveDefaultBranch()).toEqual({
    sha: '7a053e2ca07d19b2e2eebeeb0c27edaacfd67904',
    ref: 'refs/heads/master',
  });
  const lastCall = spawnGitMock.calls[spawnGitMock.calls.length - 1];
  expect(lastCall[0]).toContain('ls-remote');
});

test('resolveCommit', async () => {
  const spawnGitMock = (spawnGit: any).mock;
  const config = await Config.create();
  const git = new Git(
    config,
    {
      protocol: '',
      hostname: undefined,
      repository: '',
    },
    '',
  );
  expect(await git.resolveCommit('7a053e2')).toEqual({
    sha: '7a053e2ca07d19b2e2eebeeb0c27edaacfd67904',
    ref: undefined,
  });
  const lastCall = spawnGitMock.calls[spawnGitMock.calls.length - 1];
  expect(lastCall[0]).toContain('rev-list');
  expect(lastCall[0]).toContain('7a053e2');
});