1 | import fs from "fs";
|
2 | import path from "path";
|
3 | import assert from "assert";
|
4 | import TruffleConfig from "../dist";
|
5 | import { describe, it } from "mocha";
|
6 | import sinon from "sinon";
|
7 |
|
8 | const DEFAULT_CONFIG_FILENAME = "./test/truffle-config.js";
|
9 | const BACKUP_CONFIG_FILENAME = "./test/truffle.js";
|
10 |
|
11 | let expectedPath: string;
|
12 | let options: { config: string };
|
13 |
|
14 | describe("TruffleConfig.detect", () => {
|
15 | describe("when a config path is provided", () => {
|
16 | beforeEach(() => {
|
17 | sinon.stub(TruffleConfig, "load");
|
18 | options = { config: "/my/favorite/config.js" };
|
19 | expectedPath = "/my/favorite/config.js";
|
20 | });
|
21 | afterEach(() => {
|
22 | (TruffleConfig as any).load.restore();
|
23 | });
|
24 |
|
25 | it("loads a config from the options if available", () => {
|
26 | TruffleConfig.detect(options);
|
27 | assert((TruffleConfig as any).load.calledWith(expectedPath));
|
28 | });
|
29 | it("loads a config even with a relative path", () => {
|
30 | options.config = "../../config.js";
|
31 | TruffleConfig.detect(options);
|
32 | assert(
|
33 | (TruffleConfig as any).load.calledWith(
|
34 | path.resolve(process.cwd(), "../../config.js")
|
35 | )
|
36 | );
|
37 | });
|
38 | });
|
39 | });
|
40 |
|
41 | describe("when it can't find a config file", () => {
|
42 | beforeEach(() => {
|
43 | sinon.stub(TruffleConfig, "search").returns(null);
|
44 | });
|
45 | afterEach(() => {
|
46 | (TruffleConfig as any).search.restore();
|
47 | });
|
48 |
|
49 | it("throws if a truffle config isn't detected", () => {
|
50 | assert.throws(() => {
|
51 | TruffleConfig.detect();
|
52 | }, "should have thrown!");
|
53 | });
|
54 | });
|
55 |
|
56 | before(() => {
|
57 | fs.closeSync(fs.openSync("./test/truffle-config.js", "w"));
|
58 | fs.closeSync(fs.openSync("./test/truffle.js", "w"));
|
59 | });
|
60 |
|
61 | after(() => {
|
62 | if (fs.existsSync(DEFAULT_CONFIG_FILENAME)) {
|
63 | fs.unlinkSync(DEFAULT_CONFIG_FILENAME);
|
64 | }
|
65 |
|
66 | if (fs.existsSync(BACKUP_CONFIG_FILENAME)) {
|
67 | fs.unlinkSync(BACKUP_CONFIG_FILENAME);
|
68 | }
|
69 | });
|
70 |
|
71 | describe("TruffleConfig.search", () => {
|
72 | const options = {
|
73 | workingDirectory: `${process.cwd()}/test`
|
74 | };
|
75 |
|
76 | let loggedStuff = "";
|
77 |
|
78 | console.warn = (stringToLog: string) => {
|
79 | loggedStuff = loggedStuff + stringToLog;
|
80 | };
|
81 |
|
82 | it("returns null if passed a file that doesn't exist", () => {
|
83 | const nonExistentConfig = TruffleConfig.search(options, "badConfig.js");
|
84 | assert.strictEqual(nonExistentConfig, null);
|
85 | });
|
86 |
|
87 | it("outputs warning and returns truffle-config.js path if both truffle.js and truffle-config.js are found", () => {
|
88 | const truffleConfigPath = TruffleConfig.search(options);
|
89 |
|
90 | assert.strictEqual(
|
91 | path.normalize(truffleConfigPath!),
|
92 | path.normalize(`${process.cwd()}/test/truffle-config.js`)
|
93 | );
|
94 |
|
95 | assert(
|
96 | loggedStuff.includes(
|
97 | "Warning: Both truffle-config.js and truffle.js were found."
|
98 | )
|
99 | );
|
100 | });
|
101 |
|
102 | it("outputs warning and returns truffle.js path if only truffle.js detected on windows ", () => {
|
103 | fs.unlinkSync("./test/truffle-config.js");
|
104 |
|
105 | Object.defineProperty(process, "platform", {
|
106 | value: "win32"
|
107 | });
|
108 |
|
109 | const truffleConfigPath = TruffleConfig.search(options);
|
110 |
|
111 | assert.strictEqual(
|
112 | path.normalize(truffleConfigPath!),
|
113 | path.normalize(`${process.cwd()}/test/truffle.js`)
|
114 | );
|
115 |
|
116 | assert(loggedStuff.includes("Warning: Please rename truffle.js"));
|
117 |
|
118 | fs.closeSync(fs.openSync("./test/truffle-config.js", "w"));
|
119 | });
|
120 | });
|