diff --git a/lib/Compile.js b/lib/Compile.js index b9e36bc8..3986b89e 100644 --- a/lib/Compile.js +++ b/lib/Compile.js @@ -24,7 +24,7 @@ function compile( compileProcess.on('close', function (exitCode) { if (exitCode !== 0) { - reject('Compilation failed'); + reject(new Error(`\`elm make\` failed with exit code ${exitCode}.`)); } else { resolve(); } @@ -94,13 +94,7 @@ function compileSources( if (exitCode === 0) { resolve(); } else { - const msg = - 'Compilation failed while attempting to ' + - (testFilePaths.length > 0 - ? 'build ' + testFilePaths.join(' ') - : 'run `elm make` on ' + projectRootDir); - - reject(msg); + reject(new Error(`\`elm make\` failed with exit code ${exitCode}.`)); } }); }); @@ -138,7 +132,6 @@ function isMachineReadableReporter(reporter /*: string */) /*: boolean */ { module.exports = { compile, - compileSources, compileAll, getTestRootDir, getGeneratedCodeDir, diff --git a/lib/Generate.js b/lib/Generate.js index 70c82318..434301d7 100644 --- a/lib/Generate.js +++ b/lib/Generate.js @@ -52,20 +52,13 @@ function readUtf8(filepath) { function generateElmJson( projectRootDir /*: string */, generatedCodeDir /*: string */, - hasBeenGivenCustomGlobs /*: boolean */ + hasBeenGivenCustomGlobs /*: boolean */, + elmJsonPath /*: string */, + projectElmJson /*: any */ ) /*: [string, string, Array] */ { const testRootDir = Compile.getTestRootDir(projectRootDir); - const elmJsonPath = path.resolve(path.join(projectRootDir, 'elm.json')); const generatedSrc = path.join(generatedCodeDir, 'src'); - var projectElmJson = {}; - - try { - projectElmJson = fs.readJsonSync(elmJsonPath); - } catch (err) { - console.error('Error reading elm.json: ' + err); - process.exit(1); - } var isPackageProject = projectElmJson.type === 'package'; var dependencies = Solve.get_dependencies(elmJsonPath); diff --git a/lib/elm-test.js b/lib/elm-test.js index a2b4f7a6..daf6bcf1 100644 --- a/lib/elm-test.js +++ b/lib/elm-test.js @@ -230,9 +230,9 @@ function runTests(generatedCodeDir /*: string */, testFile /*: string */) { } ); }) - .catch(function (exitCode) { + .catch(function (error) { console.error('Compilation failed for', testFile); - return Promise.reject(exitCode); + return Promise.reject(error); }); } @@ -281,7 +281,6 @@ function getGlobsToWatch(elmJson) { }); } -let projectElmJson = {}; let report; if ( @@ -305,77 +304,53 @@ function infoLog(msg) { } } +// It's important to globify all the arguments. +// On Bash 4.x (or zsh), if you give it a glob as its last argument, Bash +// translates that into a list of file paths. On bash 3.x it's just a string. +// Ergo, globify all the arguments we receive. +const isMake = args._[0] === 'make'; +const testFileGlobs = isMake ? args._.slice(1) : args._; +const testFilePaths = resolveGlobs(testFileGlobs); const projectRootDir = process.cwd(); +const generatedCodeDir = Compile.getGeneratedCodeDir(projectRootDir); +const hasBeenGivenCustomGlobs = testFileGlobs.length > 0; -if (args._[0] === 'make') { - const files = args._.slice(1); +const elmJsonPath = path.resolve(path.join(projectRootDir, 'elm.json')); +let projectElmJson; - // It's important to globify all the arguments. - // On Bash 4.x (or zsh), if you give it a glob as its last argument, Bash - // translates that into a list of file paths. On bash 3.x it's just a string. - // Ergo, globify all the arguments we receive. - const fileGlobs = files.length > 0 ? files : []; - const testFilePaths = resolveGlobs(files); - const hasBeenGivenCustomGlobs = fileGlobs.length > 0; - const elmJsonPath = path.resolve(path.join(projectRootDir, 'elm.json')); +try { + projectElmJson = fs.readJsonSync(elmJsonPath); +} catch (err) { + console.error('Error reading elm.json: ' + err.message); + throw process.exit(1); +} - try { - projectElmJson = fs.readJsonSync(elmJsonPath); - } catch (err) { - console.error('Error reading elm.json: ' + err); - process.exit(1); - } +const isPackageProject = projectElmJson.type === 'package'; - const isPackageProject = projectElmJson.type === 'package'; +if (isMake) { + Generate.generateElmJson( + projectRootDir, + generatedCodeDir, + hasBeenGivenCustomGlobs, + elmJsonPath, + projectElmJson + ); - if (isPackageProject) { - // Run `elm make` on the project root, to generate the .elmi files. - Compile.compileSources( - [], - projectRootDir, - args.verbose, - pathToElmBinary, - args.report - ) - .then(() => - elmTestMake( - projectRootDir, - pathToElmBinary, - testFilePaths, - hasBeenGivenCustomGlobs - ) - ) - .catch((err) => { - console.error(err.message); - process.exit(1); - }); - } else { - elmTestMake( - projectRootDir, - pathToElmBinary, - testFilePaths, - hasBeenGivenCustomGlobs - ); - } + Compile.compileAll( + elmVersion, + testFilePaths, + generatedCodeDir, + args.verbose, + pathToElmBinary, + args.report + ) + .then(function () { + process.exit(0); + }) + .catch(function () { + process.exit(1); + }); } else { - // It's important to globify all the arguments. - // On Bash 4.x (or zsh), if you give it a glob as its last argument, Bash - // translates that into a list of file paths. On bash 3.x it's just a string. - // Ergo, globify all the arguments we receive. - const testFileGlobs = args._.length > 0 ? args._ : []; - const testFilePaths = resolveGlobs(testFileGlobs); - - const elmJsonPath = path.resolve(path.join(projectRootDir, 'elm.json')); - - try { - projectElmJson = fs.readJsonSync(elmJsonPath); - } catch (err) { - console.error('Error reading elm.json: ' + err); - process.exit(1); - } - - const isPackageProject = projectElmJson.type === 'package'; - if (testFilePaths.length === 0) { var extraAppError = "\n\nAlternatively, if your application has tests in a different directory, try calling elm-test with a glob: elm-test 'frontend-app/**/*Tests.elm'."; @@ -392,37 +367,12 @@ if (args._[0] === 'make') { process.exit(1); } - if (isPackageProject) { - // Run `elm make` on the project root, to generate the .elmi files. - Compile.compileSources( - [], - projectRootDir, - args.verbose, - pathToElmBinary, - args.report - ) - .then(() => generateAndRun(projectRootDir, testFileGlobs, testFilePaths)) - .catch((err) => { - console.error(err.message); - process.exit(1); - }); - } else { - generateAndRun(projectRootDir, testFileGlobs, testFilePaths); - } -} - -function generateAndRun( - projectRootDir /*: string */, - testFileGlobs /* Array */, - testFilePaths /*: Array */ -) { - const generatedCodeDir = Compile.getGeneratedCodeDir(projectRootDir); - const hasBeenGivenCustomGlobs = testFileGlobs.length > 0; - const returnValues = Generate.generateElmJson( projectRootDir, generatedCodeDir, - hasBeenGivenCustomGlobs + hasBeenGivenCustomGlobs, + elmJsonPath, + projectElmJson ); const generatedSrc = returnValues[1]; @@ -438,27 +388,20 @@ function generateAndRun( args.report ) .then(function (testModules) { - return Promise.resolve() - .then(function () { - process.chdir(generatedCodeDir); - - const mainFile = Generate.generateMainModule( - parseInt(args.fuzz), - parseInt(args.seed), - args.report, - testFileGlobs, - testFilePaths, - testModules, - generatedSrc, - processes - ); - - return runTests(generatedCodeDir, mainFile); - }) - .catch(function (err) { - console.error(err.message); - process.exit(1); - }); + process.chdir(generatedCodeDir); + + const mainFile = Generate.generateMainModule( + parseInt(args.fuzz), + parseInt(args.seed), + args.report, + testFileGlobs, + testFilePaths, + testModules, + generatedSrc, + processes + ); + + return runTests(generatedCodeDir, mainFile); }) .catch(function (err) { console.error(err.message); @@ -505,33 +448,3 @@ function generateAndRun( }); } } - -function elmTestMake( - projectRootDir /*: string */, - pathToElmBinary /*: string */, - testFilePaths /*: Array */, - hasBeenGivenCustomGlobs /*: boolean */ -) { - const generatedCodeDir = Compile.getGeneratedCodeDir(projectRootDir); - - Generate.generateElmJson( - projectRootDir, - generatedCodeDir, - hasBeenGivenCustomGlobs - ); - - Compile.compileAll( - elmVersion, - testFilePaths, - generatedCodeDir, - args.verbose, - pathToElmBinary, - args.report - ) - .then(function () { - process.exit(0); - }) - .catch(function () { - process.exit(1); - }); -} diff --git a/tests/fixtures/elm-home/0.19.1/packages/registry.dat b/tests/fixtures/elm-home/0.19.1/packages/registry.dat index 8f8e193a..fd98cb7b 100644 Binary files a/tests/fixtures/elm-home/0.19.1/packages/registry.dat and b/tests/fixtures/elm-home/0.19.1/packages/registry.dat differ diff --git a/tests/fixtures/elm-home/elm-json/versions.dat b/tests/fixtures/elm-home/elm-json/versions.dat index 35c039da..9d06075e 100644 Binary files a/tests/fixtures/elm-home/elm-json/versions.dat and b/tests/fixtures/elm-home/elm-json/versions.dat differ