1 module tests.it.runtime.dub;
2 
3 
4 import tests.it.runtime;
5 import reggae.reggae;
6 import std.path;
7 
8 
9 @("dub project with no reggaefile ninja")
10 @Tags(["dub", "ninja"])
11 unittest {
12 
13     import std.string: join;
14 
15     with(immutable ReggaeSandbox("dub")) {
16         shouldNotExist("reggaefile.d");
17         writelnUt("\n\nReggae output:\n\n", runReggae("-b", "ninja", "--dflags=-g -debug").lines.join("\n"), "-----\n");
18         shouldExist("reggaefile.d");
19         auto output = ninja.shouldExecuteOk(testPath);
20         output.shouldContain("-g -debug");
21 
22         shouldSucceed("atest").shouldEqual(
23             ["Why hello!",
24              "",
25              "I'm immortal!"]
26         );
27 
28         // there's only one UT in main.d which always fails
29         shouldFail("ut");
30     }
31 }
32 
33 @("dub project with no reggaefile tup")
34 @Tags(["dub", "tup"])
35 unittest {
36     with(immutable ReggaeSandbox("dub")) {
37         runReggae("-b", "tup", "--dflags=-g -debug").
38             shouldThrowWithMessage("dub integration not supported with the tup backend");
39     }
40 }
41 
42 @("dub project with no reggaefile and prebuild command")
43 @Tags(["dub", "ninja"])
44 unittest {
45     with(immutable ReggaeSandbox("dub_prebuild")) {
46         runReggae("-b", "ninja", "--dflags=-g -debug");
47         ninja.shouldExecuteOk(testPath);
48         shouldSucceed("ut");
49     }
50 }
51 
52 @("dub project with postbuild command")
53 @Tags(["dub", "ninja"])
54 unittest {
55     with(immutable ReggaeSandbox("dub_postbuild")) {
56         runReggae("-b", "ninja", "--dflags=-g -debug");
57         ninja.shouldExecuteOk(testPath);
58         shouldExist("foo.txt");
59         shouldSucceed("postbuild");
60     }
61 }
62 
63 
64 @("project with dependencies not on file system already no dub.selections.json")
65 @Tags(["dub", "ninja"])
66 unittest {
67 
68     import std.file: exists, rmdirRecurse;
69     import std.process: environment;
70     import std.path: buildPath;
71 
72     const cerealedDir = buildPath(environment["HOME"], ".dub/packages/cerealed-0.6.8");
73     if(cerealedDir.exists)
74         rmdirRecurse(cerealedDir);
75 
76     with(immutable ReggaeSandbox()) {
77         writeFile("dub.json", `
78 {
79   "name": "depends_on_cerealed",
80   "license": "MIT",
81   "targetType": "executable",
82   "dependencies": { "cerealed": "==0.6.8" }
83 }`);
84         writeFile("source/app.d", "void main() {}");
85 
86         runReggae("-b", "ninja");
87     }
88 }
89 
90 
91 @("simple dub project with no main function but with unit tests")
92 @Tags(["dub", "ninja"])
93 unittest {
94     import std.file: mkdirRecurse;
95     import std.path: buildPath;
96 
97     with(immutable ReggaeSandbox()) {
98         writeFile("dub.json", `
99             {
100               "name": "depends_on_cerealed",
101               "license": "MIT",
102               "targetType": "executable",
103               "dependencies": { "cerealed": "==0.6.8" }
104             }`);
105 
106         writeFile("reggaefile.d", q{
107             import reggae;
108             mixin build!(dubTestTarget!(CompilerFlags("-g -debug")));
109         });
110 
111         mkdirRecurse(buildPath(testPath, "source"));
112         writeFile("source/foo.d", `unittest { assert(false); }`);
113         runReggae("-b", "ninja");
114         ninja.shouldExecuteOk(testPath);
115 
116         shouldFail("ut");
117     }
118 }
119 
120 @("issue #23 - reggae/dub build should rebuild if dub.json/sdl change")
121 @Tags(["dub", "make"])
122 unittest {
123 
124     import std.process: execute;
125     import std.path: buildPath;
126 
127     with(immutable ReggaeSandbox("dub")) {
128         runReggae("-b", "make", "--dflags=-g -debug");
129         make.shouldExecuteOk(testPath).shouldContain("-g -debug");
130         {
131             const ret = execute(["touch", buildPath(testPath, "dub.json")]);
132             ret.status.shouldEqual(0);
133         }
134         {
135             const ret = execute(["make", "-C", testPath]);
136             // don't assert on the status of ret - it requires rerunning reggae
137             // and that can fail if the reggae binary isn't built yet.
138             // Either way -g -debug shows up in the output as the code attempts
139             // to rebuild the binary
140             ret.output.shouldContain("-g -debug");
141         }
142     }
143 }
144 
145 @("version from main package is used in dependent packages")
146 @Tags(["dub", "ninja"])
147 unittest {
148     with(immutable ReggaeSandbox()) {
149         writeFile("dub.sdl", `
150             name "foo"
151             versions "lefoo"
152             targetType "executable"
153             dependency "bar" path="bar"
154         `);
155         writeFile("source/app.d", q{
156             void main() {
157                 import bar;
158                 import std.stdio;
159                 writeln(lebar);
160             }
161         });
162         writeFile("bar/dub.sdl", `
163             name "bar"
164         `);
165         writeFile("bar/source/bar.d", q{
166             module bar;
167             version(lefoo)
168                 int lebar() { return 3; }
169             else
170                 int lebar() { return 42; }
171         });
172         runReggae("-b", "ninja");
173         ninja.shouldExecuteOk(testPath);
174         shouldSucceed("foo").shouldEqual(
175             [
176                 "3",
177             ]
178         );
179     }
180 }
181 
182 
183 @("sourceLibrary dependency")
184 @Tags(["dub", "ninja"])
185 unittest {
186     with(immutable ReggaeSandbox()) {
187         writeFile("dub.sdl", `
188             name "foo"
189             targetType "executable"
190             dependency "bar" path="bar"
191         `);
192         writeFile("source/app.d", q{
193             void main() {
194                 import bar;
195                 import std.stdio;
196                 writeln(lebar);
197             }
198         });
199         writeFile("bar/dub.sdl", `
200             name "bar"
201             targetType "sourceLibrary"
202         `);
203         writeFile("bar/source/bar.d", q{
204             module bar;
205             int lebar() { return 3; }
206         });
207         runReggae("-b", "ninja");
208         ninja.shouldExecuteOk(testPath);
209     }
210 }
211 
212 @("object source files")
213 @Tags(["dub", "ninja"])
214 unittest {
215     with(immutable ReggaeSandbox()) {
216         writeFile("dub.sdl", `
217             name "foo"
218             targetType "executable"
219             dependency "bar" path="bar"
220         `);
221         writeFile("source/app.d", q{
222             extern(C) int lebaz();
223             void main() {
224                 import bar;
225                 import std.stdio;
226                 writeln(lebar);
227                 writeln(lebaz);
228             }
229             });
230         writeFile("bar/dub.sdl", `
231             name "bar"
232             sourceFiles "../baz.o"
233         `);
234         writeFile("bar/source/bar.d", q{
235             module bar;
236             int lebar() { return 3; }
237         });
238         writeFile("baz.d", q{
239             module baz;
240             extern(C) int lebaz() { return 42; }
241         });
242 
243         ["dmd", "-c", "baz.d"].shouldExecuteOk(testPath);
244         runReggae("-b", "ninja");
245         ninja.shouldExecuteOk(testPath);
246     }
247 }