1 '''Unit tests for lib.build.'''
2 from pathlib
import Path
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
11 from lib
.build
.build
import InsideDir
, files
, exec_cmd
, _branch
, _version
, \
15 class BuildTests(TestCase
):
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
30 rmtree('test_get_files')
32 def test_exec_cmd(self
):
33 self
.assertEqual(exec_cmd('echo abc'), 'abc')
35 def test_branch(self
):
36 self
.assertIn(_branch(), ['master', 'rc', 'stable'])
38 def test_version(self
):
43 compiled
= [compile(pattern
) for pattern
in patterns
]
44 matches
= [pattern
.match(_version()) for pattern
in compiled
]
45 self
.assertTrue(any(matches
))
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']))
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 ['.', '..']]
57 self
.assertNotEqual(basename(getcwd()), dirname
)
58 with
InsideDir(dirname
):
59 self
.assertEqual(basename(getcwd()), dirname
)
60 self
.assertNotEqual(basename(getcwd()), dirname
)
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
:
66 with
open(tgt
, 'w') as ftgt
:
68 self
.assertTrue(to_be_built(tgt
, ['test_get_files/src.txt']))