Skip to content

Commit 0c533fe

Browse files
committed
[INTERNAL] Add more "init" test cases
1 parent 056fecd commit 0c533fe

File tree

14 files changed

+112
-7
lines changed

14 files changed

+112
-7
lines changed

lib/init/init.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ function getProjectType(hasWebapp, hasSrc, hasTest) {
7373
return "library";
7474
} else if (hasTest) {
7575
// Only test folder
76-
errorReason = "Found 'test' folder but no 'src' folder\n";
76+
errorReason = "Found 'test' folder but no 'src' folder.\n";
7777
} else {
7878
// No folders at all
79-
errorReason = "Could not find 'webapp' or 'src' / 'test' folders\n";
79+
errorReason = "Could not find 'webapp' or 'src' / 'test' folders.\n";
8080
}
8181
if (errorReason) {
8282
let message = `Could not detect project type: ${errorReason}`;
@@ -100,13 +100,10 @@ async function init({cwd = "./"} = {}) {
100100
specVersion: "0.1",
101101
metadata: {}
102102
};
103+
let pkg;
103104

104105
try {
105-
const pkg = await readPackageJson(path.join(cwd, "package.json"));
106-
if (!pkg.name) {
107-
throw new Error("Initialization not possible: Missing 'name' in package.json");
108-
}
109-
projectConfig.metadata.name = pkg.name;
106+
pkg = await readPackageJson(path.join(cwd, "package.json"));
110107
} catch (err) {
111108
if (err.code === "ENOENT") {
112109
throw new Error("Initialization not possible: Missing package.json file");
@@ -115,6 +112,12 @@ async function init({cwd = "./"} = {}) {
115112
}
116113
}
117114

115+
if (pkg && pkg.name) {
116+
projectConfig.metadata.name = pkg.name;
117+
} else {
118+
throw new Error("Initialization not possible: Missing 'name' in package.json");
119+
}
120+
118121
const [hasWebapp, hasSrc, hasTest] = await pathsExist(["webapp", "src", "test"], cwd);
119122
projectConfig.type = getProjectType(hasWebapp, hasSrc, hasTest);
120123

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1.0.0",
3+
"private": true,
4+
"description": "",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"keywords": [],
9+
"author": "",
10+
"license": "UNLICENSED"
11+
}

test/fixtures/init/invalid-missing-package-name/webapp/.gitkeep

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "init-invalid",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "UNLICENSED"
12+
}

test/fixtures/init/invalid-test/test/.gitkeep

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "init-invalid",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "UNLICENSED"
12+
}

test/fixtures/init/invalid-webapp-src-test/src/.gitkeep

Whitespace-only changes.

test/fixtures/init/invalid-webapp-src-test/test/.gitkeep

Whitespace-only changes.

test/fixtures/init/invalid-webapp-src-test/webapp/.gitkeep

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "init-invalid",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "UNLICENSED"
12+
}

0 commit comments

Comments
 (0)