/* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ import path from "path" import yargs from "yargs" import temp from "temp" import Command from "./command" import fs from "./fs" import type { CliOptions, RunCallback } from "./apm-cli" export default class Test extends Command { parseOptions(argv: string[]) { const options = yargs(argv).wrap(Math.min(100, yargs.terminalWidth())) options.usage(`\ Usage: apm test Runs the package's tests contained within the spec directory (relative to the current working directory).\ `) options.alias("h", "help").describe("help", "Print this usage message") return options.alias("p", "path").string("path").describe("path", "Path to atom command") } run(options: CliOptions, callback: RunCallback) { let atomCommand options = this.parseOptions(options.commandArgs) const { env } = process if (options.argv.path) { atomCommand = options.argv.path } if (!fs.existsSync(atomCommand)) { atomCommand = "atom" if (process.platform === "win32") { atomCommand += ".cmd" } } const packagePath = process.cwd() const testArgs = ["--dev", "--test", path.join(packagePath, "spec")] if (process.platform === "win32") { const logFile = temp.openSync({ suffix: ".log", prefix: `${path.basename(packagePath)}-` }) fs.closeSync(logFile.fd) const logFilePath = logFile.path testArgs.push(`--log-file=${logFilePath}`) return this.spawn(atomCommand, testArgs, function (code) { try { const loggedOutput = fs.readFileSync(logFilePath, "utf8") if (loggedOutput) { process.stdout.write(`${loggedOutput}\n`) } } catch (error) { /* ignore error */ } if (code === 0) { process.stdout.write("Tests passed\n".green) return callback() } else if (code?.message) { return callback(`Error spawning Atom: ${code.message}`) } else { return callback("Tests failed") } }) } else { return this.spawn(atomCommand, testArgs, { env, streaming: true }, function (code) { if (code === 0) { process.stdout.write("Tests passed\n".green) return callback() } else if (code?.message) { return callback(`Error spawning ${atomCommand}: ${code.message}`) } else { return callback("Tests failed") } }) } } }