Skip to content

Commit 53364f9

Browse files
author
Michael Perrotte
committedSep 27, 2019
test: added refactered tests for gists
1 parent 888f9b4 commit 53364f9

File tree

2 files changed

+299
-36
lines changed

2 files changed

+299
-36
lines changed
 

‎test/fixtures/gist.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
module.exports = [
4+
{
5+
host: function (p) { return 'gist:' + p.owner + '/' + p.project },
6+
label: 'gist'
7+
},
8+
{
9+
host: function (p) { return 'git@' + p.domain + ':/' + p.project },
10+
label: 'git@:/'
11+
},
12+
{
13+
host: function (p) { return 'git@' + p.domain + ':/' + p.project + '.git' },
14+
label: 'git@:/.git'
15+
},
16+
{
17+
host: function (p) { return 'git@' + p.domain + ':' + p.project + '.git' },
18+
label: 'git@'
19+
},
20+
{
21+
host: function (p) { return 'git@' + p.domain + ':/' + p.project + '.git' },
22+
label: 'git@/'
23+
},
24+
{
25+
host: function (p) { return 'git://' + p.domain + '/' + p.project },
26+
label: 'git'
27+
},
28+
{
29+
host: function (p) { return 'git://' + p.domain + '/' + p.project + '.git' },
30+
label: 'git.git'
31+
},
32+
{
33+
host: function (p) { return 'git://' + p.domain + '/' + p.project + '#' + p.branch },
34+
label: 'git#branch',
35+
hasBranch: true
36+
},
37+
{
38+
host: function (p) { return 'git://' + p.domain + '/' + p.project + '.git#' + p.branch },
39+
label: 'git.git#branch',
40+
hasBranch: true
41+
},
42+
{
43+
host: function (p) { return 'git://' + p.domain + ':/' + p.project },
44+
label: 'git:/'
45+
},
46+
{
47+
host: function (p) { return 'git://' + p.domain + ':/' + p.project + '.git' },
48+
label: 'git:/.git'
49+
},
50+
{
51+
host: function (p) { return 'git://' + p.domain + ':/' + p.project + '#' + p.branch },
52+
label: 'git:/#branch',
53+
hasBranch: true
54+
},
55+
{
56+
host: function (p) { return 'git://' + p.domain + ':/' + p.project + '.git#' + p.branch },
57+
label: 'git:/.git#branch',
58+
hasBranch: true
59+
}
60+
]

‎test/gist.js

+239-36
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,252 @@
22
var HostedGit = require('../index')
33
var test = require('tap').test
44

5-
test('fromUrl(gist url)', function (t) {
6-
var proj = new Array(33).join('2')
7-
function verify (host, label, branch) {
5+
var showLabel = function (label, fn) { return label + ' -> ' + fn }
6+
7+
var testFixtures = function (t, params, fixtures) {
8+
for (var i = 0; i < fixtures.length; ++i) {
9+
var fixture = fixtures[i]
10+
var host = fixture.host(params)
811
var hostinfo = HostedGit.fromUrl(host)
9-
var hash = branch ? '#' + branch : ''
10-
t.ok(hostinfo, label)
11-
if (!hostinfo) return
12-
t.is(hostinfo.https(), 'git+https://gist.github.com/' + proj + '.git' + hash, label + ' -> https')
13-
t.is(hostinfo.git(), 'git://gist.github.com/' + proj + '.git' + hash, label + ' -> git')
14-
t.is(hostinfo.browse(), 'https://gist.github.com/' + proj + (branch ? '/' + branch : ''), label + ' -> browse')
15-
t.is(hostinfo.browse('C'), 'https://gist.github.com/' + proj + (branch ? '/' + branch : '') + '#file-c', label + ' -> browse(path)')
16-
t.is(hostinfo.browse('C/D'), 'https://gist.github.com/' + proj + (branch ? '/' + branch : '') + '#file-cd', label + ' -> browse(path)')
17-
t.is(hostinfo.browse('C', 'A'), 'https://gist.github.com/' + proj + (branch ? '/' + branch : '') + '#file-c', label + ' -> browse(path, fragment)')
18-
t.is(hostinfo.browse('C/D', 'A'), 'https://gist.github.com/' + proj + (branch ? '/' + branch : '') + '#file-cd', label + ' -> browse(path)')
19-
t.is(hostinfo.bugs(), 'https://gist.github.com/' + proj, label + ' -> bugs')
20-
t.is(hostinfo.docs(), 'https://gist.github.com/' + proj + (branch ? '/' + branch : ''), label + ' -> docs')
21-
t.is(hostinfo.ssh(), 'git@gist.github.com:/' + proj + '.git' + hash, label + ' -> ssh')
22-
t.is(hostinfo.sshurl(), 'git+ssh://git@gist.github.com/' + proj + '.git' + hash, label + ' -> sshurl')
23-
t.is(hostinfo.shortcut(), 'gist:' + proj + hash, label + ' -> shortcut')
12+
13+
// INFO: from Url should return `undefined` from fixture input
14+
if (fixture.isUndefined) {
15+
t.test('input results in undefined', function (tt) {
16+
tt.is(hostinfo, undefined)
17+
tt.end()
18+
})
19+
break
20+
}
21+
22+
t.test('hostinfo.https', function (tt) {
23+
var expected = function (url, hasBranch) {
24+
return (hasBranch)
25+
? url + '#' + params.branch
26+
: url
27+
}
28+
tt.is(
29+
hostinfo.https(),
30+
expected('git+https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
31+
showLabel(fixture.label, 'https')
32+
)
33+
tt.is(
34+
hostinfo.https({ noCommittish: true }),
35+
// INFO: not using `expected` because with `{noCommittish: true}` the output is always the same
36+
'git+https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git',
37+
showLabel(fixture.label, 'https({ noCommittish: true })')
38+
)
39+
tt.is(
40+
hostinfo.https({ noGitPlus: true }),
41+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
42+
showLabel(fixture.label, 'https({ noGitPlus: true })')
43+
)
44+
tt.end()
45+
})
46+
t.test('hostinfo.git', function (tt) {
47+
var expected = function (url, hasBranch) {
48+
return (hasBranch)
49+
? url + '#' + params.branch
50+
: url
51+
}
52+
tt.is(
53+
hostinfo.git(),
54+
expected('git://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
55+
showLabel(fixture.label, 'git')
56+
)
57+
tt.is(
58+
hostinfo.git({ noCommittish: true }),
59+
// INFO: not using `expected` because with `{noCommittish: true}` the output is always the same
60+
'git://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git',
61+
showLabel(fixture.label, 'git({ noCommittish: true })')
62+
)
63+
tt.is(
64+
hostinfo.git({ noGitPlus: true }),
65+
expected('git://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
66+
showLabel(fixture.label, 'git({ noGitPlus: true })')
67+
)
68+
tt.end()
69+
})
70+
t.test('hostinfo.browse', function (tt) {
71+
var expected = function (url, hasBranch) {
72+
return (hasBranch)
73+
? url + '/' + params.branch
74+
: url
75+
}
76+
tt.is(
77+
hostinfo.browse(),
78+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch),
79+
showLabel(fixture.label, 'browse')
80+
)
81+
tt.is(
82+
hostinfo.browse('C'),
83+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch) + '#file-c',
84+
showLabel(fixture.label, "browse('C')")
85+
)
86+
tt.is(
87+
hostinfo.browse('C/D'),
88+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch) + '#file-cd',
89+
showLabel(fixture.label, "browse('C/D')")
90+
)
91+
tt.is(
92+
hostinfo.browse('C', 'A'),
93+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch) + '#file-c',
94+
showLabel(fixture.label, "browse('C', 'A')")
95+
)
96+
tt.is(
97+
hostinfo.browse('C/D', 'A'),
98+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch) + '#file-cd',
99+
showLabel(fixture.label, "browse('C/D', 'A')")
100+
)
101+
tt.end()
102+
})
103+
t.test('hostinfo.bugs', function (tt) {
104+
tt.is(
105+
hostinfo.bugs(),
106+
'https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2',
107+
showLabel(fixture.label, 'bugs')
108+
)
109+
tt.end()
110+
})
111+
t.test('hostinfo.docs', function (tt) {
112+
var expected = function (url, hasBranch) {
113+
return (hasBranch)
114+
? url + '/' + params.branch
115+
: url
116+
}
117+
tt.is(
118+
hostinfo.docs(),
119+
expected('https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch),
120+
showLabel(fixture.label, 'docs')
121+
)
122+
tt.end()
123+
})
124+
t.test('hostinfo.ssh', function (tt) {
125+
var expected = function (url, hasBranch) {
126+
return (hasBranch)
127+
? url + '#' + params.branch
128+
: url
129+
}
130+
tt.is(
131+
hostinfo.ssh(),
132+
expected('git@gist.github.com:/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
133+
showLabel(fixture.label, 'ssh')
134+
)
135+
tt.end()
136+
})
137+
t.test('hostinfo.sshurl', function (tt) {
138+
var expected = function (url, hasBranch) {
139+
return (hasBranch)
140+
? url + '#' + params.branch
141+
: url
142+
}
143+
tt.is(
144+
hostinfo.sshurl(),
145+
expected('git+ssh://git@gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
146+
showLabel(fixture.label, 'sshurl')
147+
)
148+
tt.end()
149+
})
150+
t.test('hostinfo.shortcut', function (tt) {
151+
var expected = function (url, hasBranch) {
152+
return (hasBranch)
153+
? url + '#' + params.branch
154+
: url
155+
}
156+
tt.is(
157+
hostinfo.shortcut(),
158+
expected('gist:a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2', fixture.hasBranch),
159+
showLabel(fixture.label, 'shortcut')
160+
)
161+
tt.end()
162+
})
24163
if (hostinfo.user) {
25-
t.is(hostinfo.file(''), 'https://gist.githubusercontent.com/111/' + proj + '/raw/' + (branch ? branch + '/' : ''), label + ' -> file')
26-
t.is(hostinfo.file('C'), 'https://gist.githubusercontent.com/111/' + proj + '/raw/' + (branch ? branch + '/' : '') + 'C', label + ' -> file')
27-
t.is(hostinfo.file('C/D'), 'https://gist.githubusercontent.com/111/' + proj + '/raw/' + (branch ? branch + '/' : '') + 'C/D', label + ' -> file')
28-
t.is(hostinfo.tarball(), 'https://codeload.github.com/gist/' + proj + '/tar.gz/' + (branch || 'master'), label + ' -> tarball')
29-
t.is(hostinfo.tarball({ noCommittish: true }), 'https://codeload.github.com/gist/' + proj + '/tar.gz/' + (branch || 'master'), label + ' -> tarball')
164+
t.test('hostinfo.file', function (tt) {
165+
var expected = function (url, hasBranch) {
166+
return (hasBranch)
167+
? url + params.branch + '/'
168+
: url
169+
}
170+
tt.is(
171+
hostinfo.file(),
172+
expected('https://gist.githubusercontent.com/some-owner/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/raw/', fixture.hasBranch),
173+
showLabel(fixture.label, 'file')
174+
)
175+
tt.is(
176+
hostinfo.file(''),
177+
expected('https://gist.githubusercontent.com/some-owner/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/raw/', fixture.hasBranch),
178+
showLabel(fixture.label, "file('')")
179+
)
180+
tt.is(
181+
hostinfo.file('C'),
182+
expected('https://gist.githubusercontent.com/some-owner/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/raw/', fixture.hasBranch) + 'C',
183+
showLabel(fixture.label, "file('C')")
184+
)
185+
tt.is(
186+
hostinfo.file('C/D'),
187+
expected('https://gist.githubusercontent.com/some-owner/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/raw/', fixture.hasBranch) + 'C/D',
188+
showLabel(fixture.label, "file('C/D')")
189+
)
190+
tt.is(
191+
hostinfo.file('C', 'A'),
192+
expected('https://gist.githubusercontent.com/some-owner/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/raw/', fixture.hasBranch) + 'C',
193+
showLabel(fixture.label, "file('C', 'A')")
194+
)
195+
tt.end()
196+
})
197+
t.test('hostinfo.tarball', function (tt) {
198+
var expected = function (url, hasBranch) {
199+
return (hasBranch)
200+
? url.replace(/master/gi, params.branch)
201+
: url
202+
}
203+
tt.is(
204+
hostinfo.tarball(),
205+
expected('https://codeload.github.com/gist/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/tar.gz/master', fixture.hasBranch),
206+
showLabel(fixture.label, 'tarball')
207+
)
208+
tt.is(
209+
hostinfo.tarball({ noCommittish: true }),
210+
expected('https://codeload.github.com/gist/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2/tar.gz/master', fixture.hasBranch),
211+
showLabel(fixture.label, 'tarball({ noCommittish: true })')
212+
)
213+
tt.end()
214+
})
30215
}
31-
}
32216

33-
verify('git@gist.github.com:' + proj + '.git', 'git@')
34-
var hostinfo = HostedGit.fromUrl('git@gist.github.com:/c2b12db30a49324325a3781302668408.git')
35-
if (t.ok(hostinfo, 'git@hex')) {
36-
t.is(hostinfo.https(), 'git+https://gist.github.com/c2b12db30a49324325a3781302668408.git', 'git@hex -> https')
217+
t.test('hostinfo.toString', function (tt) {
218+
var expected = function (url, hasBranch) {
219+
return (hasBranch)
220+
? url + '#' + params.branch
221+
: url
222+
}
223+
tt.is(
224+
hostinfo.toString(),
225+
expected('git+https://gist.github.com/a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2.git', fixture.hasBranch),
226+
showLabel(fixture.label, 'toString')
227+
)
228+
tt.end()
229+
})
37230
}
38-
verify('git@gist.github.com:/' + proj + '.git', 'git@/')
39-
verify('git://gist.github.com/' + proj, 'git')
40-
verify('git://gist.github.com/' + proj + '.git', 'git.git')
41-
verify('git://gist.github.com/' + proj + '#branch', 'git#branch', 'branch')
42-
verify('git://gist.github.com/' + proj + '.git#branch', 'git.git#branch', 'branch')
231+
}
232+
233+
test('fromUrl(gist url)', function (t) {
234+
var fixtures = require('./fixtures')
235+
var gistFixtures = require('./fixtures/gist')
236+
var collectedFixtures = [].concat(fixtures, gistFixtures)
43237

44-
require('./lib/standard-tests')(verify, 'gist.github.com', 'gist', proj)
238+
t.test('main fixtures', function (tt) {
239+
var params = {
240+
domain: 'gist.github.com',
241+
shortname: 'github',
242+
label: 'github',
243+
owner: 'some-owner',
244+
project: new Array(17).join('a2'),
245+
branch: 'feature-branch'
246+
}
45247

46-
verify(HostedGit.fromUrl('gist:111/' + proj).toString(), 'round-tripped shortcut')
47-
verify('gist:' + proj, 'shortened shortcut')
248+
testFixtures(tt, params, collectedFixtures)
249+
tt.end()
250+
})
48251

49252
t.end()
50253
})

0 commit comments

Comments
 (0)
Please sign in to comment.