-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.spec.js
72 lines (58 loc) · 2.05 KB
/
index.spec.js
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
import test from 'ava'
import process from 'process'
import tmp from 'tmp'
import fs from 'fs-extra'
import Serverless from 'serverless'
import ServerlessGitVariables from '../src'
function buildSls() {
const sls = new Serverless()
sls.pluginManager.addPlugin(ServerlessGitVariables)
sls.init()
return sls
}
test.beforeEach(t => {
t.context.initalDir = process.cwd()
})
test.beforeEach(t => {
t.context.tmpDir = tmp.dirSync({ unsafeCleanup: true }).name
})
test.afterEach.always(t => {
process.chdir(t.context.initalDir)
})
test('Variables are passed through', t => {
const sls = buildSls()
sls.service.custom.myVar = 'myVar'
sls.service.custom.myResoledVar = '${self:custom.myVar}' // eslint-disable-line
sls.variables.populateService()
t.is(sls.service.custom.myResoledVar, 'myVar')
})
test('Throws on bad key', t => {
const sls = buildSls()
sls.service.custom.myVar = '${git:badKey}' // eslint-disable-line
t.throws(() => {
sls.variables.populateService()
}, /Error: Git variable badKey is unknown.*/)
})
test('Throws on bad git command', t => {
fs.copySync('test/resources/simple_repo/git', `${t.context.tmpDir}/.git`)
process.chdir(t.context.tmpDir)
const sls = buildSls()
sls.service.custom.describe = '${git:describe}' // eslint-disable-line
t.throws(() => {
sls.variables.populateService()
}, /Error: Command failed: git describe/)
})
test('Inserts variables', t => {
fs.copySync('test/resources/full_repo/git', `${t.context.tmpDir}/.git`)
process.chdir(t.context.tmpDir)
const sls = buildSls()
sls.service.custom.describe = '${git:describe}' // eslint-disable-line
sls.service.custom.sha1 = '${git:sha1}' // eslint-disable-line
sls.service.custom.branch = '${git:branch}' // eslint-disable-line
sls.service.custom.describe2 = '${git:describe}' // eslint-disable-line
sls.variables.populateService()
t.is(sls.service.custom.sha1, '90440bd')
t.is(sls.service.custom.branch, 'another_branch')
t.is(sls.service.custom.describe, 'my_tag-1-g90440bd')
t.is(sls.service.custom.describe2, 'my_tag-1-g90440bd')
})