diff --git a/lib/init/init.js b/lib/init/init.js index 9236a145..a30d2def 100644 --- a/lib/init/init.js +++ b/lib/init/init.js @@ -73,10 +73,10 @@ function getProjectType(hasWebapp, hasSrc, hasTest) { return "library"; } else if (hasTest) { // Only test folder - errorReason = "Found 'test' folder but no 'src' folder\n"; + errorReason = "Found 'test' folder but no 'src' folder.\n"; } else { // No folders at all - errorReason = "Could not find 'webapp' or 'src' / 'test' folders\n"; + errorReason = "Could not find 'webapp' or 'src' / 'test' folders.\n"; } if (errorReason) { let message = `Could not detect project type: ${errorReason}`; @@ -100,13 +100,10 @@ async function init({cwd = "./"} = {}) { specVersion: "0.1", metadata: {} }; + let pkg; try { - const pkg = await readPackageJson(path.join(cwd, "package.json")); - if (!pkg.name) { - throw new Error("Initialization not possible: Missing 'name' in package.json"); - } - projectConfig.metadata.name = pkg.name; + pkg = await readPackageJson(path.join(cwd, "package.json")); } catch (err) { if (err.code === "ENOENT") { throw new Error("Initialization not possible: Missing package.json file"); @@ -115,6 +112,12 @@ async function init({cwd = "./"} = {}) { } } + if (pkg && pkg.name) { + projectConfig.metadata.name = pkg.name; + } else { + throw new Error("Initialization not possible: Missing 'name' in package.json"); + } + const [hasWebapp, hasSrc, hasTest] = await pathsExist(["webapp", "src", "test"], cwd); projectConfig.type = getProjectType(hasWebapp, hasSrc, hasTest); diff --git a/test/fixtures/init/invalid-missing-package-name/package.json b/test/fixtures/init/invalid-missing-package-name/package.json new file mode 100644 index 00000000..11d1353a --- /dev/null +++ b/test/fixtures/init/invalid-missing-package-name/package.json @@ -0,0 +1,11 @@ +{ + "version": "1.0.0", + "private": true, + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "UNLICENSED" +} diff --git a/test/fixtures/init/invalid-missing-package-name/webapp/.gitkeep b/test/fixtures/init/invalid-missing-package-name/webapp/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid-test/package.json b/test/fixtures/init/invalid-test/package.json new file mode 100644 index 00000000..a9eff2a7 --- /dev/null +++ b/test/fixtures/init/invalid-test/package.json @@ -0,0 +1,12 @@ +{ + "name": "init-invalid", + "version": "1.0.0", + "private": true, + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "UNLICENSED" +} diff --git a/test/fixtures/init/invalid-test/test/.gitkeep b/test/fixtures/init/invalid-test/test/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid-webapp-src-test/package.json b/test/fixtures/init/invalid-webapp-src-test/package.json new file mode 100644 index 00000000..a9eff2a7 --- /dev/null +++ b/test/fixtures/init/invalid-webapp-src-test/package.json @@ -0,0 +1,12 @@ +{ + "name": "init-invalid", + "version": "1.0.0", + "private": true, + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "UNLICENSED" +} diff --git a/test/fixtures/init/invalid-webapp-src-test/src/.gitkeep b/test/fixtures/init/invalid-webapp-src-test/src/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid-webapp-src-test/test/.gitkeep b/test/fixtures/init/invalid-webapp-src-test/test/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid-webapp-src-test/webapp/.gitkeep b/test/fixtures/init/invalid-webapp-src-test/webapp/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid-webapp-test/package.json b/test/fixtures/init/invalid-webapp-test/package.json new file mode 100644 index 00000000..a9eff2a7 --- /dev/null +++ b/test/fixtures/init/invalid-webapp-test/package.json @@ -0,0 +1,12 @@ +{ + "name": "init-invalid", + "version": "1.0.0", + "private": true, + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "UNLICENSED" +} diff --git a/test/fixtures/init/invalid-webapp-test/test/.gitkeep b/test/fixtures/init/invalid-webapp-test/test/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid-webapp-test/webapp/.gitkeep b/test/fixtures/init/invalid-webapp-test/webapp/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/init/invalid/package.json b/test/fixtures/init/invalid/package.json new file mode 100644 index 00000000..a9eff2a7 --- /dev/null +++ b/test/fixtures/init/invalid/package.json @@ -0,0 +1,12 @@ +{ + "name": "init-invalid", + "version": "1.0.0", + "private": true, + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "UNLICENSED" +} diff --git a/test/lib/init/init.js b/test/lib/init/init.js index c67e58a8..21c7cb3b 100644 --- a/test/lib/init/init.js +++ b/test/lib/init/init.js @@ -36,6 +36,15 @@ test("Init for library", async (t) => { }); }); +test("Init for invalid project (Found 'webapp', 'src' and 'test' folders)", async (t) => { + await t.throws(init({ + cwd: getFixturePath("invalid-webapp-src-test") + }), + "Could not detect project type: Found 'webapp', 'src' and 'test' folders.\n" + + "Applications should only have a 'webapp' folder.\n" + + "Libraries should only have a 'src' and (optional) 'test' folder."); +}); + test("Init for invalid project (Found 'webapp' and 'src' folders)", async (t) => { await t.throws(init({ cwd: getFixturePath("invalid-webapp-src") @@ -45,9 +54,43 @@ test("Init for invalid project (Found 'webapp' and 'src' folders)", async (t) => "Libraries should only have a 'src' and (optional) 'test' folder."); }); +test("Init for invalid project (Found 'webapp' and 'test' folders)", async (t) => { + await t.throws(init({ + cwd: getFixturePath("invalid-webapp-test") + }), + "Could not detect project type: Found 'webapp' and 'test' folders.\n" + + "Applications should only have a 'webapp' folder.\n" + + "Libraries should only have a 'src' and (optional) 'test' folder."); +}); + +test("Init for invalid project (Found 'test' folder but no 'src' folder)", async (t) => { + await t.throws(init({ + cwd: getFixturePath("invalid-test") + }), + "Could not detect project type: Found 'test' folder but no 'src' folder.\n" + + "Applications should only have a 'webapp' folder.\n" + + "Libraries should only have a 'src' and (optional) 'test' folder."); +}); + +test("Init for invalid project (Could not find 'webapp' or 'src' / 'test' folders)", async (t) => { + await t.throws(init({ + cwd: getFixturePath("invalid") + }), + "Could not detect project type: Could not find 'webapp' or 'src' / 'test' folders.\n" + + "Applications should only have a 'webapp' folder.\n" + + "Libraries should only have a 'src' and (optional) 'test' folder."); +}); + test("Init for invalid project (No package.json)", async (t) => { await t.throws(init({ cwd: getFixturePath("invalid-no-package-json") }), "Initialization not possible: Missing package.json file"); }); + +test("Init for invalid project (Missing 'name' in package.json)", async (t) => { + await t.throws(init({ + cwd: getFixturePath("invalid-missing-package-name") + }), + "Initialization not possible: Missing 'name' in package.json"); +});