aboutsummaryrefslogtreecommitdiff
path: root/tests/BuildInfoTest.php
blob: 93cb130eee2e1df9b69fe120f11005652ef97bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php

require_once("licenses/BuildInfo.php");

class BuildInfoTest extends PHPUnit_Framework_TestCase
{

    private $temp_filename;
    private $good_bi;
    private $empty_bi;
    private $fname;
    private $lic_text_test = 
        '<p>IMPORTANT — PLEASE READ THE FOLLOWING AGREEMENT CAREFULLY.</p>';

    public function setUp()
    {
        $this->good_bi = new BuildInfo("tests/BUILD-INFO.txt");
        $this->temp_filename = tempnam(sys_get_temp_dir(), "build-info");
        $this->empty_bi = new BuildInfo($this->temp_filename);
        $this->fname = "BUILD-INFO.txt";
    }

    public function tearDown() {
        if (file_exists($this->temp_filename)) {
            unlink($this->temp_filename);
        }
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function test_parseLine_fails() {
        $line = "no separator";
        $buildinfo = new BuildInfo("");
        $buildinfo->parseLine($line);
    }

    public function test_parseLine_passes() {
        $line = "Build-Name:value";
        $buildinfo = new BuildInfo("");
        $this->assertEquals(array("Build-Name" => "value"),
                            $buildinfo->parseLine($line));
    }

    public function test_parseLine_trims() {
        $line = "Build-Name: value";
        $buildinfo = new BuildInfo("");
        $this->assertEquals(array("Build-Name" => "value"),
                            $buildinfo->parseLine($line));
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function test_parseLine_invalid_field() {
        $line = "field: value";
        $buildinfo = new BuildInfo("");
        $this->assertEquals(array("field" => "value"),
                            $buildinfo->parseLine($line));
    }

    public function test_isValidField_true() {
        $buildinfo = new BuildInfo("");
        $fields_allowed = array("Format-Version", "Files-Pattern",
                                "Build-Name", "Theme", "License-Type", "OpenID-Launchpad-Teams",
                                "Collect-User-Data", "License-Text");
        foreach ($fields_allowed as $field) {
            $this->assertTrue($buildinfo->isValidField($field));
        }
    }

    public function test_isValidField_false() {
        $buildinfo = new BuildInfo("");
        $this->assertFalse($buildinfo->isValidField("Some random text"));
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function test_parseData_fails() {
        $buildinfo = new BuildInfo("");
        $buildinfo->parseData(array("Arbitrary text"));
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function test_parseData_array_expected() {
        $buildinfo = new BuildInfo("");
        $buildinfo->parseData("Arbitrary text");
    }

    public function test_parseData_format_version() {
        $buildinfo = new BuildInfo("");
        $values = $buildinfo->parseData(array("Format-Version: 2.0"));
        $this->assertEquals(array("Format-Version" => "2.0"),
                            $values);
    }

    public function test_parseData_extra_fields() {
        $buildinfo = new BuildInfo("");
        $values = $buildinfo->parseData(array(
                                              "Format-Version: 2.0",
                                              "Files-Pattern: *.txt",
                                              "Build-Name: woohoo"));
        $this->assertEquals(array("Format-Version" => "2.0",
                                  "*.txt" => array("Build-Name" => "woohoo")),
                            $values);
    }

    public function test_parseBlock_license() {
        $buildinfo = new BuildInfo("");
        $lineno = 0;
        $values = $buildinfo->parseBlock(array(
                                              "Format-Version: 2.0",
                                              "License-Text: line1",
                                              " line2"), $lineno);
        $this->assertEquals(array("Format-Version" => "2.0",
                                  "License-Text" => "line1\nline2"),
                            $values);
    }

    public function test_parseContinuation_no_continuation() {
        $buildinfo = new BuildInfo("");
        $lineno = 0;
        $this->assertEquals(
            "",
            $buildinfo->parseContinuation(array("no-space"), $lineno));
    }

    public function test_parseContinuation_indexed() {
        $buildinfo = new BuildInfo("");
        $lineno = 0;
        $this->assertEquals("",
                            $buildinfo->parseContinuation(array("no-space", " space"), $lineno));
    }

    public function test_parseContinuation() {
        $buildinfo = new BuildInfo("");
        $lineno = 1;
        $value = $buildinfo->parseContinuation(array("no-space", " line1", " line2"), $lineno);
        $this->assertEquals("\nline1\nline2", $value);
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function test_parseData_no_format_version_fails() {
        $buildinfo = new BuildInfo("");
        $values = $buildinfo->parseData(array("Build-Name: blah"));
    }

   public function test_parseData_blocks() {
        $buildinfo = new BuildInfo("");
        $lineno = 0;
        $values = $buildinfo->parseData(array("Format-Version: 2.0",
                                              "Files-Pattern: *.txt",
                                              "Build-Name: woohoo",
                                              "Files-Pattern: *.tgz",
                                              "Build-Name: weehee"));
        $this->assertEquals(array("Format-Version" => "2.0",
                                  "*.txt" => array("Build-Name" => "woohoo"),
                                  "*.tgz" => array("Build-Name" => "weehee")),
                            $values);
   }
 
    public function test_parseData_block_multiple_patterns() {
        $buildinfo = new BuildInfo("");
        $lineno = 0;
        $values = $buildinfo->parseData(array("Format-Version: 2.0",
                                              "Files-Pattern: *.txt,*.tgz",
                                              "Build-Name: weehee"));
        $this->assertEquals(array("Format-Version" => "2.0",
                                  "*.txt" => array("Build-Name" => "weehee"),
                                  "*.tgz" => array("Build-Name" => "weehee")),
                            $values);
    }
 
   
    /**
     * Running readFile on a directory returns false.
     */
    public function test_readFile_nonFile()
    {
        $bi = new BuildInfo(dirname(__FILE__));
        $this->assertFalse($bi->readFile());
    }

    /**
     * Running readFile on a nonexistent file returns false.
     */
    public function test_readFile_nonexistentFile()
    {
        $bi = new BuildInfo("nonexistent.file");
        $this->assertFalse($bi->readFile());
    }

    /**
     * Running readFile on a regular file returns array of strings.
     */
    public function test_readFile_file()
    {
        $bi = new BuildInfo("tests/BUILD-INFO.txt");
        $this->assertInternalType('array', $bi->readFile());
    }

    /**
     * Running 'get' functions on an empty fields returns false.
     */
    public function test_getFormatVersion_empty()
    {
        $this->assertFalse($this->empty_bi->getFormatVersion());
    }

    /**
     * Running 'get' functions on non-empty fields returns string value.
     */
    public function test_getFormatVersion_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getFormatVersion());
    }

    public function test_getFormatVersion()
    {
        $this->assertEquals('0.1', $this->good_bi->getFormatVersion());
    }

    public function test_getBuildName_empty()
    {
        $this->assertFalse($this->empty_bi->getBuildName($this->fname));
    }

    public function test_getBuildName_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getBuildName($this->fname));
    }

    public function test_getBuildName()
    {
        $this->assertEquals(
            'landing-snowball', $this->good_bi->getBuildName($this->fname));
    }

    public function test_getTheme_empty()
    {
        $this->assertFalse($this->empty_bi->getTheme($this->fname));
    }

    public function test_getTheme_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getTheme($this->fname));
    }

    public function test_getTheme()
    {
        $this->assertEquals(
            'stericsson', $this->good_bi->getTheme($this->fname));
    }

    public function test_getLicenseType_empty()
    {
        $this->assertFalse($this->empty_bi->getLicenseType($this->fname));
    }

    public function test_getLicenseType_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getLicenseType($this->fname));
    }

    public function test_getLicenseType()
    {
        $this->assertEquals(
            'open', $this->good_bi->getLicenseType($this->fname));
    }

    public function test_getCollectUserData_empty()
    {
        $this->assertFalse($this->empty_bi->getCollectUserData($this->fname));
    }

    public function test_getCollectUserData_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getCollectUserData($this->fname));
    }

    public function test_getCollectUserData()
    {
        $this->assertEquals(
            'yes', $this->good_bi->getCollectUserData($this->fname));
    }

    public function test_getLaunchpadTeams_empty()
    {
        $this->assertFalse($this->empty_bi->getLaunchpadTeams($this->fname));
    }

    public function test_getLaunchpadTeams_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getLaunchpadTeams($this->fname));
    }

    public function test_getLaunchpadTeams()
    {
        $this->assertEquals(
            'linaro,non-linaro',
            $this->good_bi->getLaunchpadTeams($this->fname));
    }

    public function test_getLicenseText_empty()
    {
        $this->assertFalse($this->empty_bi->getLicenseText($this->fname));
    }

    public function test_getLicenseText_type()
    {
        $this->assertInternalType(
            'string', $this->good_bi->getLicenseText($this->fname));
    }

    public function test_getLicenseText()
    {
        $this->assertStringStartsWith(
            $this->lic_text_test,
            $this->good_bi->getLicenseText($this->fname));
    }
}
?>