Skip to content

Commit

Permalink
test: extract modifyPackageJson to simplify test code
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Mar 13, 2016
1 parent 72bd5e9 commit d1801ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
28 changes: 12 additions & 16 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "./helpers/avaEx"
import { assertPack } from "./helpers/packTester"
import { assertPack, modifyPackageJson } from "./helpers/packTester"
import { move, writeJson, readJson } from "fs-extra-p"
import { Promise as BluebirdPromise } from "bluebird"
import * as path from "path"
Expand All @@ -24,15 +24,13 @@ test("custom app dir", async () => {
await assertPack("test-app-one", getPossiblePlatforms(), {
// speed up tests, we don't need check every arch
arch: process.arch
}, true, async (projectDir) => {
const file = path.join(projectDir, "package.json")
const data = await readJson(file)
data.directories = {
buildResources: "custom"
}

return await BluebirdPromise.all([
writeJson(file, data),
}, true, (projectDir) => {
return BluebirdPromise.all([
modifyPackageJson(projectDir, data => {
data.directories = {
buildResources: "custom"
}
}),
move(path.join(projectDir, "build"), path.join(projectDir, "custom"))
])
})
Expand All @@ -42,12 +40,10 @@ test("productName with space", async () => {
await assertPack("test-app-one", getPossiblePlatforms(), {
// speed up tests, we don't need check every arch
arch: process.arch
}, true, async (projectDir) => {
const file = path.join(projectDir, "package.json")
const data = await readJson(file)
data.productName = "Test App"

return await writeJson(file, data)
}, true, (projectDir) => {
return modifyPackageJson(projectDir, data => {
data.productName = "Test App"
})
})
})

Expand Down
9 changes: 8 additions & 1 deletion test/src/helpers/packTester.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copy, emptyDir, remove } from "fs-extra-p"
import { copy, emptyDir, remove, writeJson, readJson } from "fs-extra-p"
import * as assertThat from "should/as-function"
import * as path from "path"
import { parse as parsePlist } from "plist"
Expand Down Expand Up @@ -164,4 +164,11 @@ async function getContents(path: string, productName: string) {
.map(it => it.length === 0 ? null : it.substring(it.indexOf(".") + 1))
.filter(it => it != null && !(it.startsWith(`/opt/${productName}/locales/`) || it.startsWith(`/opt/${productName}/libgcrypt`)))
)
}

export async function modifyPackageJson(projectDir: string, task: (data: any) => void): Promise<any> {
const file = path.join(projectDir, "package.json")
const data = await readJson(file)
task(data)
return await writeJson(file, data)
}

0 comments on commit d1801ae

Please sign in to comment.