Debian Codegraph @

extra/haxelib_src/test/IntegrationTests.hx

raw
1 import haxe.unit.*;
2 import haxe.*;
3 import haxe.io.*;
4 import sys.*;
5 import sys.io.*;
6 import haxelib.*;
7 using StringTools;
8 using IntegrationTests;
9
10 typedef UserRegistration = {
11 user:String,
12 email:String,
13 fullname:String,
14 pw:String
15 }
16
17 class IntegrationTests extends TestBase {
18 static var projectRoot:String = Sys.getCwd();
19 var haxelibBin:String = Path.join([projectRoot, "run.n"]);
20 public var server(default, null):String = switch (Sys.getEnv("HAXELIB_SERVER")) {
21 case null:
22 "localhost";
23 case url:
24 url;
25 };
26 public var serverPort(default, null) = switch (Sys.getEnv("HAXELIB_SERVER_PORT")) {
27 case null:
28 2000;
29 case port:
30 Std.parseInt(port);
31 };
32 public var serverUrl(get, null):String;
33 function get_serverUrl() return serverUrl != null ? serverUrl : serverUrl = 'http://${server}:${serverPort}/';
34
35 static var originalRepo(default, never) = {
36 var p = new Process("haxelib", ["config"]);
37 var originalRepo = Path.normalize(p.stdout.readLine());
38 p.close();
39 if (repo == originalRepo) {
40 throw "haxelib repo is the same as test repo: " + repo;
41 }
42 originalRepo;
43 };
44 static public var repo(default, never) = "repo_integration_tests";
45 static public var bar(default, never):UserRegistration = {
46 user: "Bar",
47 email: "[email protected]",
48 fullname: "Bar",
49 pw: "barpassword",
50 };
51 static public var foo(default, never):UserRegistration = {
52 user: "Foo",
53 email: "[email protected]",
54 fullname: "Foo",
55 pw: "foopassword",
56 };
57 static public var deepAuthor(default, never):UserRegistration = {
58 user: "DeepAuthor",
59 email: "[email protected]",
60 fullname: "Jonny Deep",
61 pw: "deep thought"
62 }
63 static public var anotherGuy(default, never):UserRegistration = {
64 user: "AnotherGuy",
65 email: "[email protected]",
66 fullname: "Another Guy",
67 pw: "some other pw"
68 }
69 public var clientVer(get, null):SemVer;
70 var clientVer_inited = false;
71 function get_clientVer() {
72 return if (clientVer_inited)
73 clientVer;
74 else {
75 clientVer = {
76 var r = haxelib(["version"]).result();
77 if (r.code == 0)
78 SemVer.ofString(switch(r.out.trim()) {
79 case _.split(" ") => parts: parts[0];
80 case v: v;
81 });
82 else if (r.out.indexOf("3.1.0-rc.4") >= 0)
83 SemVer.ofString("3.1.0-rc.4");
84 else
85 throw "unknown version";
86 };
87 clientVer_inited = true;
88 clientVer;
89 }
90 }
91
92 function haxelib(args:Array<String>, ?input:String):Process {
93 var p = #if system_haxelib
94 new Process("haxelib", ["-R", serverUrl].concat(args));
95 #else
96 new Process("neko", [haxelibBin, "-R", serverUrl].concat(args));
97 #end
98
99 if (input != null) {
100 p.stdin.writeString(input);
101 p.stdin.close();
102 }
103
104 return p;
105 }
106
107 function assertSuccess(r:{out:String, err:String, code:Int}, ?pos:haxe.PosInfos):Void {
108 if (r.code != 0) {
109 throw r;
110 }
111 assertEquals(0, r.code, pos);
112 }
113
114 function assertFail(r:{out:String, err:String, code:Int}, ?pos:haxe.PosInfos):Void {
115 assertTrue(r.code != 0, pos);
116 }
117
118 function assertNoError(f:Void->Void):Void {
119 f();
120 assertTrue(true);
121 }
122
123 var dbConfig:Dynamic = Json.parse(File.getContent("www/dbconfig.json"));
124 var dbCnx:sys.db.Connection;
125 function resetDB():Void {
126 var db = dbConfig.database;
127 dbCnx.request('DROP DATABASE IF EXISTS ${db};');
128 dbCnx.request('CREATE DATABASE ${db};');
129
130 var filesPath = "www/files/3.0";
131 for (item in FileSystem.readDirectory(filesPath)) {
132 if (item.endsWith(".zip")) {
133 FileSystem.deleteFile(Path.join([filesPath, item]));
134 }
135 }
136 var tmpPath = "tmp";
137 for (item in FileSystem.readDirectory(filesPath)) {
138 if (item.endsWith(".tmp")) {
139 FileSystem.deleteFile(Path.join([tmpPath, item]));
140 }
141 }
142 }
143
144 override function setup():Void {
145 super.setup();
146
147 dbCnx = sys.db.Mysql.connect({
148 user: dbConfig.user,
149 pass: dbConfig.pass,
150 host: server,
151 port: dbConfig.port,
152 database: dbConfig.database,
153 });
154 resetDB();
155
156 deleteDirectory(repo);
157 FileSystem.createDirectory(repo);
158 haxelibSetup(repo);
159
160 Sys.setCwd(Path.join([projectRoot, "test"]));
161 }
162
163 override function tearDown():Void {
164 Sys.setCwd(projectRoot);
165
166 haxelibSetup(originalRepo);
167 deleteDirectory(repo);
168
169 resetDB();
170 dbCnx.close();
171
172 super.tearDown();
173 }
174
175 static public function result(p:Process):{out:String, err:String, code:Int} {
176 var out = p.stdout.readAll().toString();
177 var err = p.stderr.readAll().toString();
178 var code = p.exitCode();
179 p.close();
180 return {out:out, err:err, code:code};
181 }
182
183 static public function haxelibSetup(path:String):Void {
184 HaxelibTests.runCommand("haxelib", ["setup", path]);
185 }
186
187 static function main():Void {
188 var prevDir = Sys.getCwd();
189
190 var runner = new TestRunner();
191 runner.add(new tests.integration.TestEmpty());
192 runner.add(new tests.integration.TestSetup());
193 runner.add(new tests.integration.TestSimple());
194 runner.add(new tests.integration.TestUpgrade());
195 runner.add(new tests.integration.TestUpdate());
196 runner.add(new tests.integration.TestList());
197 runner.add(new tests.integration.TestSet());
198 runner.add(new tests.integration.TestInfo());
199 runner.add(new tests.integration.TestUser());
200 runner.add(new tests.integration.TestOwner());
201 runner.add(new tests.integration.TestDev());
202 runner.add(new tests.integration.TestRun());
203 runner.add(new tests.integration.TestPath());
204 var success = runner.run();
205
206 if (!success) {
207 Sys.exit(1);
208 }
209 }
210 }