-
Notifications
You must be signed in to change notification settings - Fork 106
/
test-output.js
74 lines (53 loc) · 2.06 KB
/
test-output.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
73
const fs = require('fs')
const assert = require('assert')
const objectPath = require('object-path')
const yaml = require('yaml')
const toml = require('@iarna/toml')
const actionConfig = yaml.parse(fs.readFileSync('./action.yml', 'utf8'))
const {
FILES = actionConfig.inputs['version-file'].default,
EXPECTED_VERSION_PATH = actionConfig.inputs['version-path'].default,
EXPECTED_VERSION = actionConfig.inputs['fallback-version'].default
} = process.env
assert.ok(FILES, 'Files not defined!')
/**
* Test if all the files are updated
*/
FILES.split(',').map((file, index) => {
console.log(`Going to test file "${file}"`)
const fileContent = fs.readFileSync(file.trim(), 'utf8')
const fileExtension = file.split('.').pop()
assert.ok(fileExtension, 'No file extension found!')
let parsedContent = null
switch (fileExtension.toLowerCase()) {
case 'json':
parsedContent = JSON.parse(fileContent)
break
case 'yaml':
case 'yml':
parsedContent = yaml.parse(fileContent)
break
case 'toml':
parsedContent = toml.parse(fileContent)
break
case 'exs':
parsedContent = fileContent
break
default:
assert.fail(`File extension "${fileExtension}" is not supported!`)
}
console.log(`"${file}" has a valid extension "${fileExtension.toLowerCase()}"`)
assert.ok(parsedContent, 'Content could not be parsed!')
console.log(`"${file}" has valid content`, parsedContent)
const expectedVersions = EXPECTED_VERSION.split(',')
const expectedVersion = expectedVersions.length > 0
? expectedVersions[index]
: expectedVersions
if (fileExtension.toLowerCase() === 'exs') {
assert.strictEqual(parsedContent.includes(expectedVersion), true, 'Version does not match what is expected')
return
}
const newVersionInFile = objectPath.get(parsedContent, EXPECTED_VERSION_PATH, null)
console.log(`"${file}" check if "${newVersionInFile}" matches what is expected "${expectedVersion.trim()}"`)
assert.strictEqual(newVersionInFile, expectedVersion.trim(), 'Version does not match what is expected')
})