forked from curlconverter/curlconverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·177 lines (162 loc) · 4.73 KB
/
test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import test from 'tape'
import fs from 'fs'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import * as utils from './util.js'
import * as curlconverter from './index.js'
// The curl_commands/ directory contains input files
// The file name is a description of the command.
// For each input file, there may be a corresponding output file in
// language-specific directories: node/, php/, python/, parser/
// we get a list of all input files, iterate over it, and if an
// output file exists, compare the output.
const outputs = [
{
name: 'Ansible',
extension: 'yml',
command: curlconverter.toAnsible
}, {
name: 'R',
extension: 'r',
command: curlconverter.toR
},
{
name: 'Python',
extension: 'py',
command: curlconverter.toPython
},
{
name: 'Browser',
extension: 'js',
command: curlconverter.toBrowser
},
{
name: 'Node',
extension: 'js',
command: curlconverter.toNodeRequest
},
{
name: 'PHP',
extension: 'php',
command: curlconverter.toPhp
},
{
name: 'Go',
extension: 'go',
command: curlconverter.toGo
},
{
name: 'Rust',
extension: 'rs',
command: curlconverter.toRust
},
{
name: 'Strest',
extension: 'strest.yml',
command: curlconverter.toStrest
},
{
name: 'Json',
extension: 'json',
command: curlconverter.toJsonString
},
{
name: 'Dart',
extension: 'dart',
command: curlconverter.toDart
},
{
name: 'Elixir',
extension: 'ex',
command: curlconverter.toElixir
},
{
name: 'MATLAB',
extension: 'm',
command: curlconverter.toMATLAB
}, {
name: 'Java',
extension: 'java',
command: curlconverter.toJava
}
]
const languageNames = outputs.map(o => o.name.toLowerCase())
languageNames.push('parser')
const expectedParserOutputs = {}
for (const filename of fs.readdirSync('./fixtures/parser/')) {
// Import expected parser outputs once. If we try to do this in the tests, tape will produce
// "test exited without ending" errors.
const expectedOutputJSON = fs.readFileSync('./fixtures/parser/' + filename)
const expectedOutput = JSON.parse(expectedOutputJSON)
expectedParserOutputs[filename.replace(/\.json$/, '')] = expectedOutput
}
const testFile = fileName => {
const inputFilePath = 'fixtures/curl_commands/' + fileName
const inputFileContents = fs.readFileSync(inputFilePath, 'utf-8')
const parserTestName = 'Parser: ' + fileName.replace(/_/g, ' ').replace(/\.sh$/, '')
const parserOutName = fileName.replace(/\.sh/, '')
if (Object.prototype.hasOwnProperty.call(expectedParserOutputs, parserOutName)) {
const goodParserOutput = expectedParserOutputs[parserOutName]
const parsedCommand = utils.parseCurlCommand(inputFileContents)
test(parserTestName, t => {
t.deepEquals(parsedCommand, goodParserOutput)
t.end()
})
}
outputs.forEach(output => {
if (languages && !languages.includes(output.name.toLowerCase())) {
console.log(`skipping language: ${output.name}`)
} else {
const directory = './fixtures/' + output.name.toLowerCase() + '/'
const filePath = directory + fileName.replace(/\.sh$/, '.' + output.extension)
const testName = output.name + ': ' + fileName.replace(/_/g, ' ').replace(/\.sh$/, '')
if (fs.existsSync(filePath)) {
// normalize code for just \n line endings (aka fix input under Windows)
const goodCode = fs.readFileSync(filePath, 'utf-8').replace(/\r\n/g, '\n')
const code = output.command(inputFileContents)
test(testName, t => {
t.equal(code, goodCode)
t.end()
})
}
}
})
}
const testArgs = yargs(hideBin(process.argv))
.scriptName('test.js')
.usage('Usage: $0 [--language <language>] [--test <test_name>] [test_name...]')
.option('l', {
alias: 'language',
choices: languageNames,
demandOption: false,
describe: 'the language to convert the curl command to',
type: 'string'
})
.option('test', {
demandOption: false,
describe: 'the name of a file in fixtures/curl_commands without the .sh extension',
type: 'string'
})
.alias('h', 'help')
.help()
.argv
let languages
if (testArgs.language) {
languages = Array.isArray(testArgs.language) ? testArgs.language : [testArgs.language]
}
let testNames = testArgs._.slice(1)
if (Array.isArray(testArgs.test)) {
testNames = testNames.concat(testArgs.test)
} else if (typeof testArgs.test === 'string') {
testNames.push(testArgs.test)
}
if (testNames && testNames.length) {
for (const testName of testNames) {
const fileName = testName.replace(/ /g, '_') + '.sh'
testFile(fileName)
}
} else {
// otherwise, run them all
const inputFiles = fs.readdirSync('fixtures/curl_commands/')
inputFiles.map(testFile)
}