Commit | Line | Data |
---|---|---|
0f9675ba FC |
1 | '''Unit tests for lib.build.''' |
2 | from pathlib import Path | |
3 | import sys | |
4 | if '' in sys.path: sys.path.remove('') | |
5 | sys.path.append(str(Path(__file__).parent.parent.parent.parent)) | |
6 | from os import getcwd, makedirs, walk | |
7 | from os.path import basename | |
8 | from shutil import rmtree | |
9 | from unittest import TestCase | |
10 | from re import compile | |
53ddf3c3 | 11 | from ya2.build.build import InsideDir, files, exec_cmd, _branch, _version, \ |
0f9675ba FC |
12 | to_be_built |
13 | ||
14 | ||
15 | class BuildTests(TestCase): | |
16 | ||
17 | def setUp(self): | |
18 | makedirs('test_get_files/a') | |
19 | makedirs('test_get_files/b') | |
20 | makedirs('test_get_files/c') | |
21 | with open('test_get_files/a/c.ext1', 'w') as ftest: | |
22 | ftest.write('0123456789') | |
23 | with open('test_get_files/a/d.ext2', 'w'): pass | |
24 | with open('test_get_files/b/e.ext2', 'w') as ftest: | |
25 | ftest.write('0123456789') | |
26 | with open('test_get_files/b/f.ext3', 'w'): pass | |
27 | with open('test_get_files/c/g.ext2', 'w'): pass | |
28 | ||
29 | def tearDown(self): | |
30 | rmtree('test_get_files') | |
31 | ||
32 | def test_exec_cmd(self): | |
33 | self.assertEqual(exec_cmd('echo abc'), 'abc') | |
34 | ||
35 | def test_branch(self): | |
36 | self.assertIn(_branch(), ['master', 'rc', 'stable']) | |
37 | ||
38 | def test_version(self): | |
39 | patterns = [ | |
40 | "^0a[0-9]+$", | |
41 | "^0rc[0-9]+$", | |
42 | "^0\.[0-9]+$"] | |
43 | compiled = [compile(pattern) for pattern in patterns] | |
44 | matches = [pattern.match(_version()) for pattern in compiled] | |
45 | self.assertTrue(any(matches)) | |
46 | ||
47 | def test_get_files(self): | |
48 | _files = files(['ext2'], 'c') | |
49 | self.assertSetEqual(set(_files), | |
50 | set(['./test_get_files/a/d.ext2', | |
51 | './test_get_files/b/e.ext2'])) | |
52 | ||
53 | def test_inside_dir(self): | |
54 | dirs = [basename(x[0]) for x in walk('.')] | |
55 | dirs = [dir_ for dir_ in dirs if dir_ not in ['.', '..']] | |
56 | dirname = dirs[0] | |
57 | self.assertNotEqual(basename(getcwd()), dirname) | |
58 | with InsideDir(dirname): | |
59 | self.assertEqual(basename(getcwd()), dirname) | |
60 | self.assertNotEqual(basename(getcwd()), dirname) | |
61 | ||
62 | def test_to_be_built(self): | |
63 | tgt = 'test_get_files/tgt.txt' | |
64 | with open('test_get_files/src.txt', 'w') as fsrc: | |
65 | fsrc.write('src') | |
66 | with open(tgt, 'w') as ftgt: | |
67 | ftgt.write('tgt') | |
68 | self.assertTrue(to_be_built(tgt, ['test_get_files/src.txt'])) |