UNPKG

3.77 kBPlain TextView Raw
1import DockerParser from '../src/DockerParser'
2import { Person, SoftwareEnvironment } from '@stencila/schema'
3import { fixture } from './test-functions'
4import MockUrlFetcher from './MockUrlFetcher'
5
6const urlFetcher = new MockUrlFetcher()
7
8/**
9 * When passed Dockerfile strings, parse should extract LABEL
10 * and MAINTAINER instructions.
11 */
12test('parse:strings', async () => {
13 const parser = new DockerParser(urlFetcher, '')
14 let environ
15
16 environ = await parser.parse('FROM ubuntu') as SoftwareEnvironment
17 expect(environ.authors).toEqual([])
18
19 // Single label
20 environ = await parser.parse('LABEL maintainer="Joe Bloggs <joe@bloggs.com> (https://bloggs.com/joe)"') as SoftwareEnvironment
21 expect(environ.authors && environ.authors[0].name).toEqual('Joe Bloggs')
22 const author = environ.authors && environ.authors[0] as Person
23 expect(author).toBeDefined()
24 expect(author && author.givenNames).toEqual(['Joe'])
25 expect(author && author.familyNames).toEqual(['Bloggs'])
26 expect(author && author.emails).toEqual(['joe@bloggs.com'])
27 expect(author && author.urls).toEqual(['https://bloggs.com/joe'])
28
29 // Mutiple labels
30 environ = await parser.parse('LABEL maintainer="Joe Bloggs" description="My image"') as SoftwareEnvironment
31 expect(environ.description).toEqual('My image')
32 expect(environ.authors && environ.authors[0].name).toEqual('Joe Bloggs')
33
34 // Mutiple labels using same key - last wins
35 environ = await parser.parse('LABEL maintainer="Joe Bloggs" maintainer="Peter Pan"') as SoftwareEnvironment
36 expect(environ.authors && environ.authors[0].name).toEqual('Peter Pan')
37
38 // Muti-label with back slashes for spaces and line continuations
39 environ = await parser.parse('LABEL maintainer=Joe\\ Bloggs \\\ndescription="My image"') as SoftwareEnvironment
40 expect(environ.description).toEqual('My image')
41 expect(environ.authors && environ.authors[0].name).toEqual('Joe Bloggs')
42
43 // Prefixed labels
44 for (let prefix of ['org.opencontainers.image', 'org.label-schema']) {
45 environ = await parser.parse(`LABEL ${prefix}.description="My image"`) as SoftwareEnvironment
46 expect(environ.description).toEqual('My image')
47 }
48
49 // Deprecated MAINTAINER instruction
50 environ = await parser.parse('MAINTAINER Peter Pan') as SoftwareEnvironment
51 expect(environ.authors && environ.authors[0].name).toEqual('Peter Pan')
52
53 // Using multiple MAINTAINERS does result in multiple authors
54 environ = await parser.parse('LABEL maintainer="Joe Bloggs"\n MAINTAINER Peter Pan\n MAINTAINER Capt Hook') as SoftwareEnvironment
55 expect(environ.authors && environ.authors.length).toEqual(3)
56 expect(environ.authors && environ.authors[0].name).toEqual('Joe Bloggs')
57 expect(environ.authors && environ.authors[1].name).toEqual('Peter Pan')
58 expect(environ.authors && environ.authors[2].name).toEqual('Capt Hook')
59})
60
61/**
62 * When applied to an empty folder, parse should return null.
63 */
64test('parse:empty', async () => {
65 const parser = new DockerParser(urlFetcher, fixture('empty'))
66 expect(await parser.parse()).toBeNull()
67})
68
69/**
70 * When applied to a folder without a Dockerfile, parse should return null.
71 */
72test('parse:r-date', async () => {
73 const parser = new DockerParser(urlFetcher, fixture('r-date'))
74 expect(await parser.parse()).toBeNull()
75})
76
77/**
78 * When applied to a folder with a Dockerfile, parse should return a
79 * `SoftwareEnvironment` based upon it.
80 */
81test('parse:dockerfile-date', async () => {
82 const parser = new DockerParser(urlFetcher, fixture('dockerfile-date'))
83 const environ = await parser.parse() as SoftwareEnvironment
84 expect(environ.description && environ.description.substring(0, 23)).toEqual('Prints the current date')
85 expect(environ.authors && environ.authors[0].name).toEqual('Nokome Bentley')
86})