Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
import fs from 'fs'
import { dirname, resolve } from 'path'
import { execSync } from 'child_process'
import { cwd } from 'process'
import { fileURLToPath } from 'url'
import JSZip from 'jszip'
import test from 'ava'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
test.afterEach.always((t) => {
// remove files
try {
if (t.context.inputPath) fs.unlinkSync(t.context.inputPath)
} catch (err) {}
try {
if (t.context.outputPath) fs.unlinkSync(t.context.outputPath)
} catch (err) {}
try {
if (t.context.outputPath2) fs.unlinkSync(t.context.outputPath2)
} catch (err) {}
try {
if (t.context.outputPath3) fs.unlinkSync(t.context.outputPath3)
} catch (err) {}
try {
if (t.context.folderPath) fs.rmdirSync(t.context.folderPath, { recursive: false })
} catch (err) {}
})
test.beforeEach((t) => {
const cliName = './cli.js'
t.context = {
cliPath: resolve(cwd(), 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, multipart = false) => {
const jscadScript = `// test script ${id}
import { arc, ellipse, ellipsoid } from '@jscad/modeling'
export const getParameterDefinitions = () => {
return [
{ name: 'segments', caption: 'Segments:', type: 'int', initial: 10, min: 5, max: 20, step: 1 }
]
}
export const main = (params) => {
// parameters
let segments = params.segments || 16
// shapes
let apath2 = arc()
let ageom2 = ellipse()
let ageom3 = ellipsoid()
${multipart ? 'return [ageom3, ageom3, ageom3]' : 'return [apath2, ageom2, ageom3]'}
}
`
const fileName = `./test${id}.js`
const filePath = resolve(cwd(), fileName)
fs.writeFileSync(filePath, jscadScript)
return filePath
}
test('cli (single input file)', (t) => {
const testID = 1
const inputPath = createJscad(testID)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = `./test${testID}.stl`
const outputPath = resolve(cwd(), outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath}`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
})
test('cli (single input file, output format)', (t) => {
const testID = 2
const inputPath = createJscad(testID)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = `./test${testID}.dxf`
const outputPath = resolve(cwd(), outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath} -f dxf`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
})
test('cli (single input file, output filename)', (t) => {
const testID = 3
const inputPath = createJscad(testID)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = `./test${testID}.obj`
const outputPath = resolve(cwd(), outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath} -o ${outputPath}`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
})
test('cli (folder, output format)', (t) => {
const testID = 4
const inputPath = createJscad(testID)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const folderPath = resolve(cwd(), './test-folder')
t.false(fs.existsSync(folderPath))
fs.mkdirSync(folderPath)
t.true(fs.existsSync(folderPath))
t.context.folderPath = folderPath
const mainPath = resolve(cwd(), './test-folder/main.js')
fs.renameSync(inputPath, mainPath)
t.true(fs.existsSync(mainPath))
t.context.inputPath = mainPath
const outputName = './test-folder/main.dxf'
const outputPath = resolve(cwd(), outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${folderPath} -f dxf`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
})
test('cli (single input file, parameters)', (t) => {
const testID = 5
const inputPath = createJscad(testID)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = `./test${testID}.stl`
const outputPath = resolve(cwd(), outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath} -- --segments 32 --nothing "Yes"`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
})
test('cli (no parameters, out help)', (t) => {
const cliPath = t.context.cliPath
const cmd = `node ${cliPath}`
t.throws(() => {
execSync(cmd, { stdio: [0, 1, 2] })
})
})
test('cli (single input file, invalid jscad)', (t) => {
const testID = 6
const inputPath = createJscad(testID)
fs.writeFileSync(inputPath, 'INVALID')
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath}`
t.throws(() => {
execSync(cmd, { stdio: [0, 1, 2] })
})
})
test('cli (single input file, multiple output files)', (t) => {
const testID = 7
const inputPath = createJscad(testID, true)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = (partNum) => `./test${testID}-part-${partNum}-of-3.stl`
const outputPath1 = resolve(__dirname, outputName(1))
const outputPath2 = resolve(__dirname, outputName(2))
const outputPath3 = resolve(__dirname, outputName(3))
t.false(fs.existsSync(outputPath1))
t.false(fs.existsSync(outputPath2))
t.false(fs.existsSync(outputPath3))
t.context.outputPath = outputPath1
t.context.outputPath2 = outputPath2
t.context.outputPath3 = outputPath3
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} -p ${inputPath}`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath1))
t.true(fs.existsSync(outputPath2))
t.true(fs.existsSync(outputPath3))
})
test('cli (single multipart input file, zipped output file)', async (t) => {
const testID = 8
const inputPath = createJscad(testID, true)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = `./test${testID}.zip`
const outputPath = resolve(__dirname, outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath} -p -z`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
// check contents of zip file
const data = await fs.promises.readFile(outputPath)
const content = await JSZip.loadAsync(data)
t.true(content.files[resolve(__dirname, `./test${testID}-part-1-of-3.stl`)] !== undefined)
t.true(content.files[resolve(__dirname, `./test${testID}-part-2-of-3.stl`)] !== undefined)
t.true(content.files[resolve(__dirname, `./test${testID}-part-3-of-3.stl`)] !== undefined)
})
test('cli (single input file, zipped output file)', async (t) => {
const testID = 9
const inputPath = createJscad(testID, true)
t.true(fs.existsSync(inputPath))
t.context.inputPath = inputPath
const outputName = `./test${testID}.zip`
const outputPath = resolve(__dirname, outputName)
t.false(fs.existsSync(outputPath))
t.context.outputPath = outputPath
const cliPath = t.context.cliPath
const cmd = `node ${cliPath} ${inputPath} -z`
execSync(cmd, { stdio: [0, 1, 2] })
t.true(fs.existsSync(outputPath))
// check contents of zip file
const data = await fs.promises.readFile(outputPath)
const content = await JSZip.loadAsync(data)
t.true(content.files[resolve(__dirname, `./test${testID}.stl`)] !== undefined)
})
db621ec01This file is pinned to the selected release. Download the original to open it in your design tool.