Skip to content

Commit

Permalink
[INTERNAL] Add more "init" test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Oct 9, 2018
1 parent 056fecd commit 0c533fe
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand All @@ -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");
Expand All @@ -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);

Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/init/invalid-missing-package-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0.0",
"private": true,
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "UNLICENSED"
}
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/init/invalid-test/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/init/invalid-webapp-src-test/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Empty file.
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/init/invalid-webapp-test/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/init/invalid/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
43 changes: 43 additions & 0 deletions test/lib/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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");
});

0 comments on commit 0c533fe

Please sign in to comment.