1 | import { fixture } from './test-functions'
|
2 | import JavascriptParser from '../src/JavascriptParser'
|
3 | import { Person, SoftwarePackage } from '@stencila/schema'
|
4 |
|
5 | import MockUrlFetcher from './MockUrlFetcher'
|
6 |
|
7 | const urlFetcher = new MockUrlFetcher()
|
8 |
|
9 |
|
10 |
|
11 | jest.setTimeout(30 * 60 * 1000)
|
12 | describe('JavascriptParser', () => {
|
13 | |
14 |
|
15 |
|
16 | test('parse:empty', async () => {
|
17 | const parser = new JavascriptParser(urlFetcher, fixture('empty'))
|
18 | expect(await parser.parse()).toBeNull()
|
19 | })
|
20 |
|
21 | |
22 |
|
23 |
|
24 | test('parse:non-js', async () => {
|
25 | const parser = new JavascriptParser(urlFetcher, fixture('r-date'))
|
26 | expect(await parser.parse()).toBeNull()
|
27 | })
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | test('parse:js-requirements', async () => {
|
35 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements'))
|
36 | const pkg = await parser.parse() as SoftwarePackage
|
37 |
|
38 | expect(pkg.name).toEqual('js-package')
|
39 | expect(pkg.license).toEqual('ISC')
|
40 | expect(pkg.authors).toEqual([Person.fromText('Jason Bloggs <j.bloggs@example.com> (https://jbloggs.example.com)')])
|
41 | expect(pkg.codeRepository).toEqual('https://github.com/stencila/dockter/')
|
42 | expect(pkg.softwareRequirements.length).toEqual(5)
|
43 | const expecteds = [
|
44 | ['is-array', '1.0.1'],
|
45 | ['mkdirp', '0.5.1'],
|
46 | ['rimraf', '2.6.2'],
|
47 | ['array-swap', '0.0.2'],
|
48 | ['a-package-that-is-not-on-npm', 'org/repo']
|
49 | ]
|
50 |
|
51 | for (let index in expecteds) {
|
52 | let { name, version } = pkg.softwareRequirements[index]
|
53 | expect(name).toEqual(expecteds[index][0])
|
54 | expect(version).toEqual(expecteds[index][1])
|
55 | }
|
56 | })
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 | test('parse:js-requirements', async () => {
|
63 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-author-string'))
|
64 | const pkg = await parser.parse() as SoftwarePackage
|
65 |
|
66 | expect(pkg.authors).toEqual([Person.fromText('Jason Bloggs <j.bloggs@example.com> (https://jbloggs.example.com)')])
|
67 | })
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | test('parse:js-requirements-github-shortcut', async () => {
|
75 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-github-shortcut'))
|
76 | const pkg = await parser.parse() as SoftwarePackage
|
77 | expect(pkg.codeRepository).toEqual('https://github.com/stencila/dockter/')
|
78 | })
|
79 | test('parse:js-requirements-gitlab-shortcut', async () => {
|
80 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-gitlab-shortcut'))
|
81 | const pkg = await parser.parse() as SoftwarePackage
|
82 | expect(pkg.codeRepository).toEqual('https://gitlab.com/stencila/dockter/')
|
83 | })
|
84 | test('parse:js-requirements-bitbucket-shortcut', async () => {
|
85 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-bitbucket-shortcut'))
|
86 | const pkg = await parser.parse() as SoftwarePackage
|
87 | expect(pkg.codeRepository).toEqual('https://bitbucket.com/stencila/dockter/')
|
88 | })
|
89 | test('parse:js-requirements-npm-shortcut', async () => {
|
90 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-npm-shortcut'))
|
91 | const pkg = await parser.parse() as SoftwarePackage
|
92 | expect(pkg.codeRepository).toEqual('https://www.npmjs.com/package/@stencila/dockter/')
|
93 | })
|
94 | test('parse:js-requirements-repo-string', async () => {
|
95 | const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-repo-string'))
|
96 | const pkg = await parser.parse() as SoftwarePackage
|
97 | expect(pkg.codeRepository).toEqual('https://website.com/users/project/')
|
98 | })
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 | test('parse:js-sources', async () => {
|
106 | const parser = new JavascriptParser(urlFetcher, fixture('js-sources'))
|
107 | const pkg = await parser.parse() as SoftwarePackage
|
108 |
|
109 | expect(pkg.name).toEqual('js-sources')
|
110 | expect(pkg.softwareRequirements.length).toEqual(2)
|
111 | expect(pkg.softwareRequirements[1].name).toEqual('array-swap')
|
112 | expect(pkg.softwareRequirements[0].name).toEqual('is-sorted')
|
113 | })
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 | test('parse:js-mixed', async () => {
|
120 | const parser = new JavascriptParser(urlFetcher, fixture('js-mixed'))
|
121 | const pkg = await parser.parse() as SoftwarePackage
|
122 |
|
123 | expect(pkg.name).toEqual('js-mixed')
|
124 | expect(pkg.description).toEqual('A test Node.js package for js-mixed')
|
125 | expect(pkg.version).toEqual('1.2.3')
|
126 | expect(pkg.softwareRequirements.length).toEqual(1)
|
127 | expect(pkg.softwareRequirements[0].name).toEqual('is-even')
|
128 | expect(pkg.softwareRequirements[0].version).toEqual('4.5.6')
|
129 | })
|
130 | })
|