forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.conversions.test.js
More file actions
337 lines (241 loc) · 8.26 KB
/
cli.conversions.test.js
File metadata and controls
337 lines (241 loc) · 8.26 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const test = require('ava')
const path = require('path')
const { execSync } = require('child_process')
const fs = require('fs')
test.afterEach.always((t) => {
// remove files
try {
if (t.context.file1Path) fs.unlinkSync(t.context.file1Path)
} catch (err) { }
try {
if (t.context.file2Path) fs.unlinkSync(t.context.file2Path)
} catch (err) { }
try {
if (t.context.file3Path) fs.unlinkSync(t.context.file3Path)
} catch (err) { }
try {
if (t.context.file4Path) fs.unlinkSync(t.context.file4Path)
} catch (err) { }
try {
if (t.context.file5Path) fs.unlinkSync(t.context.file5Path)
} catch (err) { }
})
test.beforeEach((t) => {
const cliName = './cli.js'
t.context = {
cliPath: path.resolve(__dirname, cliName)
}
})
// CORE FUNCTIONALITY
// These tests create input and output files.
// create a simple JSCAD script for input
// the script should produce ALL geometry types
const createJscad = (id) => {
const jscadScript = `// test script ${id}
const { flatten } = require('@jscad/array-utils')
const { primitives } = require('@jscad/modeling')
const getParameterDefinitions = () => {
return flatten([
{ name: 'segments', caption: 'Segements:', type: 'int', initial: 10, min: 5, max: 20, step: 1 }
])
}
const main = (params) => {
// parameters
let segments = params.segments || 16
// shapes
let apath2 = primitives.arc()
let ageom2 = primitives.ellipse()
let ageom3 = primitives.ellipsoid()
return [apath2, ageom2, ageom3]
}
module.exports = { main, getParameterDefinitions }
`
const fileName = `./test${id}.jscad`
const filePath = path.resolve(__dirname, fileName)
fs.writeFileSync(filePath, jscadScript)
return filePath
}
const createImportJscad = (id, extension) => {
const jscadScript = `// test script ${id} -- import
const main = () => {
return require('./test${id}.${extension}')
}
module.exports = { main }
`
const fileName = `./test${id}-import.jscad`
const filePath = path.resolve(__dirname, fileName)
fs.writeFileSync(filePath, jscadScript)
return filePath
}
const testBackImport = (t, testID, extension) => {
const cliPath = t.context.cliPath
const file4Path = createImportJscad(testID, extension)
t.context.file4Path = file4Path
t.true(fs.existsSync(file4Path))
const file5Name = `./test${testID}-import.stl`
const file5Path = path.resolve(__dirname, file5Name)
t.context.file5Path = file5Path
t.false(fs.existsSync(file5Path))
const cmd = `node ${cliPath} ${file4Path}`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file5Path))
}
const runOnFixture = (t, fixtureName) => {
const inputFile = path.resolve(
__dirname,
path.join('test_fixtures', fixtureName, 'index.js'))
const outputFile = inputFile.replace('.js', '.stl')
t.context.file1Path = outputFile
t.false(fs.existsSync(outputFile))
const cmd = `node ${t.context.cliPath} ${inputFile}`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputFile))
return outputFile
}
test('cli (conversions STL)', (t) => {
const testID = 11
// convert from JSCAD script to STL
const file1Path = createJscad(testID)
t.true(fs.existsSync(file1Path))
t.context.file1Path = file1Path
const file2Name = `./test${testID}.stl`
const file2Path = path.resolve(__dirname, file2Name)
t.false(fs.existsSync(file2Path))
t.context.file2Path = file2Path
const cliPath = t.context.cliPath
let cmd = `node ${cliPath} ${file1Path}`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file2Path))
// convert from STL to JSCAD script
const file3Name = `./test${testID}.js`
const file3Path = path.resolve(__dirname, file3Name)
t.false(fs.existsSync(file3Path))
t.context.file3Path = file3Path
cmd = `node ${cliPath} ${file2Path} -o ${file3Path} -v -add-metadata false`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file3Path))
testBackImport(t, testID, 'stl')
})
test('cli (conversions DXF)', (t) => {
const testID = 12
// convert from JSCAD to DXF
const file1Path = createJscad(testID)
t.true(fs.existsSync(file1Path))
t.context.file1Path = file1Path
const file2Name = `./test${testID}.dxf`
const file2Path = path.resolve(__dirname, file2Name)
t.false(fs.existsSync(file2Path))
t.context.file2Path = file2Path
const cliPath = t.context.cliPath
let cmd = `node ${cliPath} ${file1Path} -of dxf`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file2Path))
// convert from DXF to JS
const file3Name = `./test${testID}.js`
const file3Path = path.resolve(__dirname, file3Name)
t.false(fs.existsSync(file3Path))
t.context.file3Path = file3Path
cmd = `node ${cliPath} ${file2Path} -of js`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file3Path))
testBackImport(t, testID, 'dxf')
})
test('cli (conversions AMF)', (t) => {
const testID = 13
// convert from JSCAD to AMF
const file1Path = createJscad(testID)
t.true(fs.existsSync(file1Path))
t.context.file1Path = file1Path
const file2Name = `./test${testID}.amf`
const file2Path = path.resolve(__dirname, file2Name)
t.false(fs.existsSync(file2Path))
t.context.file2Path = file2Path
const cliPath = t.context.cliPath
let cmd = `node ${cliPath} ${file1Path} -of amf`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file2Path))
// convert from AMF to JS
const file3Name = `./test${testID}.js`
const file3Path = path.resolve(__dirname, file3Name)
t.false(fs.existsSync(file3Path))
t.context.file3Path = file3Path
cmd = `node ${cliPath} ${file2Path} -of js`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file3Path))
testBackImport(t, testID, 'amf')
})
test('cli (conversions JSON)', (t) => {
const testID = 14
// convert from JSCAD to JSON
const file1Path = createJscad(testID)
t.true(fs.existsSync(file1Path))
t.context.file1Path = file1Path
const file2Name = `./test${testID}.json`
const file2Path = path.resolve(__dirname, file2Name)
t.false(fs.existsSync(file2Path))
t.context.file2Path = file2Path
const cliPath = t.context.cliPath
let cmd = `node ${cliPath} ${file1Path} -of json`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file2Path))
// convert from JSON to JS
const file3Name = `./test${testID}.js`
const file3Path = path.resolve(__dirname, file3Name)
t.false(fs.existsSync(file3Path))
t.context.file3Path = file3Path
cmd = `node ${cliPath} ${file2Path} -of js`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file3Path))
testBackImport(t, testID, 'json')
})
test('cli (conversions SVG)', (t) => {
const testID = 15
// convert from JSCAD to SVG
const file1Path = createJscad(testID)
t.true(fs.existsSync(file1Path))
t.context.file1Path = file1Path
const file2Name = `./test${testID}.svg`
const file2Path = path.resolve(__dirname, file2Name)
t.false(fs.existsSync(file2Path))
t.context.file2Path = file2Path
const cliPath = t.context.cliPath
let cmd = `node ${cliPath} ${file1Path} -of svg`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file2Path))
// convert from SVG to JS
const file3Name = `./test${testID}.js`
const file3Path = path.resolve(__dirname, file3Name)
t.false(fs.existsSync(file3Path))
t.context.file3Path = file3Path
cmd = `node ${cliPath} ${file2Path} -of js`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file3Path))
})
test('cli (conversions X3D)', (t) => {
const testID = 16
// convert from JSCAD to X3D
const file1Path = createJscad(testID)
t.true(fs.existsSync(file1Path))
t.context.file1Path = file1Path
const file2Name = `./test${testID}.x3d`
const file2Path = path.resolve(__dirname, file2Name)
t.false(fs.existsSync(file2Path))
t.context.file2Path = file2Path
const cliPath = t.context.cliPath
let cmd = `node ${cliPath} ${file1Path} -of x3d`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file2Path))
// convert from X3D to JS
const file3Name = `./test${testID}.js`
const file3Path = path.resolve(__dirname, file3Name)
t.false(fs.existsSync(file3Path))
t.context.file3Path = file3Path
cmd = `node ${cliPath} ${file2Path} -of js`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(file3Path))
testBackImport(t, testID, 'x3d')
})
test('cli (import STL)', (t) => {
// const testID = 17
runOnFixture(t, 'stl_import')
})