|
| 1 | +'use strict' |
| 2 | +const HostedGit = require('../index') |
| 3 | +const t = require('tap') |
| 4 | + |
| 5 | +const invalid = [ |
| 6 | + // missing project |
| 7 | + 'https://git.sr.ht/~foo', |
| 8 | + // invalid protocos |
| 9 | + 'git://[email protected]:~foo/bar', |
| 10 | + 'ssh://git.sr.ht:~foo/bar', |
| 11 | + // tarball url |
| 12 | + 'https://git.sr.ht/~foo/bar/archive/main.tar.gz' |
| 13 | +] |
| 14 | + |
| 15 | +// assigning the constructor here is hacky, but the only way to make assertions that compare |
| 16 | +// a subset of properties to a found object pass as you would expect |
| 17 | +const GitHost = require('../git-host') |
| 18 | +const defaults = { constructor: GitHost, type: 'sourcehut', user: '~foo', project: 'bar' } |
| 19 | + |
| 20 | +const valid = { |
| 21 | + // shortucts |
| 22 | + 'sourcehut:~foo/bar': { ...defaults, default: 'shortcut' }, |
| 23 | + 'sourcehut:~foo/bar#branch': { ...defaults, default: 'shortcut', committish: 'branch' }, |
| 24 | + |
| 25 | + // shortcuts (.git) |
| 26 | + 'sourcehut:~foo/bar.git': { ...defaults, default: 'shortcut' }, |
| 27 | + 'sourcehut:~foo/bar.git#branch': { ...defaults, default: 'shortcut', committish: 'branch' }, |
| 28 | + |
| 29 | + // no-protocol git+ssh |
| 30 | + '[email protected]:~foo/bar': { ... defaults, default: 'sshurl', auth: null }, |
| 31 | + '[email protected]:~foo/bar#branch': { ... defaults, default: 'sshurl', auth: null, committish: 'branch' }, |
| 32 | + |
| 33 | + // no-protocol git+ssh (.git) |
| 34 | + '[email protected]:~foo/bar.git': { ... defaults, default: 'sshurl', auth: null }, |
| 35 | + '[email protected]:~foo/bar.git#branch': { ... defaults, default: 'sshurl', auth: null, committish: 'branch' }, |
| 36 | + |
| 37 | + // git+ssh urls |
| 38 | + 'git+ssh://[email protected]:~foo/bar': { ... defaults, default: 'sshurl' }, |
| 39 | + 'git+ssh://[email protected]:~foo/bar#branch': { ... defaults, default: 'sshurl', committish: 'branch' }, |
| 40 | + |
| 41 | + // git+ssh urls (.git) |
| 42 | + 'git+ssh://[email protected]:~foo/bar.git': { ... defaults, default: 'sshurl' }, |
| 43 | + 'git+ssh://[email protected]:~foo/bar.git#branch': { ... defaults, default: 'sshurl', committish: 'branch' }, |
| 44 | + |
| 45 | + // https urls |
| 46 | + 'https://git.sr.ht/~foo/bar': { ...defaults, default: 'https' }, |
| 47 | + 'https://git.sr.ht/~foo/bar#branch': { ...defaults, default: 'https', committish: 'branch' }, |
| 48 | + |
| 49 | + 'https://git.sr.ht/~foo/bar.git': { ...defaults, default: 'https' }, |
| 50 | + 'https://git.sr.ht/~foo/bar.git#branch': { ...defaults, default: 'https', committish: 'branch' } |
| 51 | +} |
| 52 | + |
| 53 | +t.test('valid urls parse properly', t => { |
| 54 | + t.plan(Object.keys(valid).length) |
| 55 | + for (const [url, result] of Object.entries(valid)) { |
| 56 | + t.hasStrict(HostedGit.fromUrl(url), result, `${url} parses`) |
| 57 | + } |
| 58 | +}) |
| 59 | + |
| 60 | +t.test('invalid urls return undefined', t => { |
| 61 | + t.plan(invalid.length) |
| 62 | + for (const url of invalid) { |
| 63 | + t.equal(HostedGit.fromUrl(url), undefined, `${url} returns undefined`) |
| 64 | + } |
| 65 | +}) |
| 66 | + |
| 67 | +t.test('toString respects defaults', t => { |
| 68 | + const sshurl = HostedGit.fromUrl('git+ssh://git.sr.ht/~foo/bar') |
| 69 | + t.equal(sshurl.default, 'sshurl', 'got the right default') |
| 70 | + t.equal(sshurl.toString(), sshurl.sshurl(), 'toString calls sshurl') |
| 71 | + |
| 72 | + const https = HostedGit.fromUrl('https://git.sr.ht/~foo/bar') |
| 73 | + t.equal(https.default, 'https', 'got the right default') |
| 74 | + t.equal(https.toString(), https.https(), 'toString calls https') |
| 75 | + |
| 76 | + const shortcut = HostedGit.fromUrl('sourcehut:~foo/bar') |
| 77 | + t.equal(shortcut.default, 'shortcut', 'got the right default') |
| 78 | + t.equal(shortcut.toString(), shortcut.shortcut(), 'toString calls shortcut') |
| 79 | + |
| 80 | + t.end() |
| 81 | +}) |
| 82 | + |
| 83 | +t.test('string methods populate correctly', t => { |
| 84 | + const parsed = HostedGit.fromUrl('git+ssh://git.sr.ht/~foo/bar') |
| 85 | + t.equal(parsed.getDefaultRepresentation(), parsed.default, 'getDefaultRepresentation()') |
| 86 | + t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset') |
| 87 | + t.equal(parsed.ssh(), '[email protected]:~foo/bar.git') |
| 88 | + t.equal(parsed.sshurl(), 'git+ssh://[email protected]/~foo/bar.git') |
| 89 | + t.equal(parsed.browse(), 'https://git.sr.ht/~foo/bar') |
| 90 | + t.equal(parsed.browse('/lib/index.js'), 'https://git.sr.ht/~foo/bar/tree/main/lib/index.js') |
| 91 | + t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://git.sr.ht/~foo/bar/tree/main/lib/index.js#l100') |
| 92 | + t.equal(parsed.docs(), 'https://git.sr.ht/~foo/bar#readme') |
| 93 | + t.equal(parsed.https(), 'https://git.sr.ht/~foo/bar.git') |
| 94 | + t.equal(parsed.shortcut(), 'sourcehut:~foo/bar') |
| 95 | + t.equal(parsed.path(), '~foo/bar') |
| 96 | + t.equal(parsed.tarball(), 'https://git.sr.ht/~foo/bar/archive/main.tar.gz') |
| 97 | + t.equal(parsed.file(), 'https://git.sr.ht/~foo/bar/blob/main/') |
| 98 | + t.equal(parsed.file('/lib/index.js'), 'https://git.sr.ht/~foo/bar/blob/main/lib/index.js') |
| 99 | + t.equal(parsed.bugs(), 'https://todo.sr.ht/~foo/bar') |
| 100 | + |
| 101 | + t.equal(parsed.docs({ committish: 'fix/bug' }), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug#readme', 'allows overriding options') |
| 102 | + |
| 103 | + t.same(parsed.git(), null, 'git() returns null') |
| 104 | + |
| 105 | + const extra = HostedGit.fromUrl('https://@git.sr.ht/~foo/bar#fix/bug') |
| 106 | + t.equal(extra.hash(), '#fix/bug') |
| 107 | + t.equal(extra.https(), 'https://git.sr.ht/~foo/bar.git#fix/bug') |
| 108 | + t.equal(extra.shortcut(), 'sourcehut:~foo/bar#fix/bug') |
| 109 | + t.equal(extra.ssh(), '[email protected]:~foo/bar.git#fix/bug') |
| 110 | + t.equal(extra.sshurl(), 'git+ssh://[email protected]/~foo/bar.git#fix/bug') |
| 111 | + t.equal(extra.browse(), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug') |
| 112 | + t.equal(extra.browse('/lib/index.js'), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug/lib/index.js') |
| 113 | + t.equal(extra.browse('/lib/index.js', 'L200'), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug/lib/index.js#l200') |
| 114 | + t.equal(extra.docs(), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug#readme') |
| 115 | + t.equal(extra.file(), 'https://git.sr.ht/~foo/bar/blob/fix%2Fbug/') |
| 116 | + t.equal(extra.file('/lib/index.js'), 'https://git.sr.ht/~foo/bar/blob/fix%2Fbug/lib/index.js') |
| 117 | + |
| 118 | + t.equal(extra.sshurl({ noCommittish: true }), 'git+ssh://[email protected]/~foo/bar.git', 'noCommittish drops committish from urls') |
| 119 | + t.equal(extra.sshurl({ noGitPlus: true }), 'ssh://[email protected]/~foo/bar.git#fix/bug', 'noGitPlus drops git+ prefix from urls') |
| 120 | + |
| 121 | + t.end() |
| 122 | +}) |
0 commit comments