blob: 2815b211fa2a782a7975256113192f9675cd8e17 [file] [log] [blame]
Alex Deymo3cfb9cd2014-08-18 15:56:35 -07001#!/usr/bin/python
2# Copyright 2014 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for dep_tracker.py."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070010import os
Mike Frysinger16c51062014-09-05 23:34:29 -040011import sys
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070012
Mike Frysinger16c51062014-09-05 23:34:29 -040013sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
14 '..', '..'))
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070015from chromite.lib import cros_test_lib
Alex Deymo365b10c2014-08-25 13:14:28 -070016from chromite.lib import osutils
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070017from chromite.lib import unittest_lib
18from chromite.scripts import dep_tracker
19
20# Allow access private members for testing:
21# pylint: disable=W0212
22
23
24class MainTest(cros_test_lib.MoxOutputTestCase):
25 """Tests for the main() function."""
26
27 def testHelp(self):
28 """Test that --help is functioning."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050029 argv = ['--help']
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070030
31 with self.OutputCapturer() as output:
32 # Running with --help should exit with code==0.
33 self.AssertFuncSystemExitZero(dep_tracker.main, argv)
34
35 # Verify that a message beginning with "usage: " was printed.
36 stdout = output.GetStdout()
37 self.assertTrue(stdout.startswith('usage: '))
38
39
40class DepTrackerTest(cros_test_lib.TempDirTestCase):
41 """Tests for the DepTracker() class."""
42
43 def testSimpleDep(self):
44 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
45 ['func_a', 'func_b', 'func_c'])
46 unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'),
47 undefined_symbols=['func_b'],
48 used_libs=['abc'],
49 executable=True)
50 dt = dep_tracker.DepTracker(self.tempdir)
51 dt.Init()
52 dt.ComputeELFFileDeps()
53
54 self.assertEquals(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
55
Alex Deymo365b10c2014-08-25 13:14:28 -070056 def testFiletypeSet(self):
57 """Tests that the 'ftype' member is set for ELF files first."""
58 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
59 ['func_a', 'func_b', 'func_c'])
60 osutils.WriteFile(os.path.join(self.tempdir, 'pyscript'),
61 "#!/usr/bin/python\nimport sys\nsys.exit(42)\n")
62 dt = dep_tracker.DepTracker(self.tempdir)
63 dt.Init()
64
65 # ComputeELFFileDeps() should compute the file type of ELF files so we
66 # don't need to parse them again.
67 dt.ComputeELFFileDeps()
68 self.assertTrue('ftype' in dt._files['libabc.so'])
69 self.assertFalse('ftype' in dt._files['pyscript'])
70
71 # ComputeFileTypes() shold compute the file type of every file.
72 dt.ComputeFileTypes()
73 self.assertTrue('ftype' in dt._files['libabc.so'])
74 self.assertTrue('ftype' in dt._files['pyscript'])
75
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070076
77if __name__ == '__main__':
78 cros_test_lib.main()