blob: d098ca8843d3dffb786c6f1ef3a3576bfe86244f [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
8import os
Mike Frysinger16c51062014-09-05 23:34:29 -04009import sys
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070010
Mike Frysinger16c51062014-09-05 23:34:29 -040011sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
12 '..', '..'))
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070013from chromite.lib import cros_test_lib
Alex Deymo365b10c2014-08-25 13:14:28 -070014from chromite.lib import osutils
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070015from chromite.lib import unittest_lib
16from chromite.scripts import dep_tracker
17
18# Allow access private members for testing:
19# pylint: disable=W0212
20
21
22class MainTest(cros_test_lib.MoxOutputTestCase):
23 """Tests for the main() function."""
24
25 def testHelp(self):
26 """Test that --help is functioning."""
27 argv = [ '--help' ]
28
29 with self.OutputCapturer() as output:
30 # Running with --help should exit with code==0.
31 self.AssertFuncSystemExitZero(dep_tracker.main, argv)
32
33 # Verify that a message beginning with "usage: " was printed.
34 stdout = output.GetStdout()
35 self.assertTrue(stdout.startswith('usage: '))
36
37
38class DepTrackerTest(cros_test_lib.TempDirTestCase):
39 """Tests for the DepTracker() class."""
40
41 def testSimpleDep(self):
42 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
43 ['func_a', 'func_b', 'func_c'])
44 unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'),
45 undefined_symbols=['func_b'],
46 used_libs=['abc'],
47 executable=True)
48 dt = dep_tracker.DepTracker(self.tempdir)
49 dt.Init()
50 dt.ComputeELFFileDeps()
51
52 self.assertEquals(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
53
Alex Deymo365b10c2014-08-25 13:14:28 -070054 def testFiletypeSet(self):
55 """Tests that the 'ftype' member is set for ELF files first."""
56 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
57 ['func_a', 'func_b', 'func_c'])
58 osutils.WriteFile(os.path.join(self.tempdir, 'pyscript'),
59 "#!/usr/bin/python\nimport sys\nsys.exit(42)\n")
60 dt = dep_tracker.DepTracker(self.tempdir)
61 dt.Init()
62
63 # ComputeELFFileDeps() should compute the file type of ELF files so we
64 # don't need to parse them again.
65 dt.ComputeELFFileDeps()
66 self.assertTrue('ftype' in dt._files['libabc.so'])
67 self.assertFalse('ftype' in dt._files['pyscript'])
68
69 # ComputeFileTypes() shold compute the file type of every file.
70 dt.ComputeFileTypes()
71 self.assertTrue('ftype' in dt._files['libabc.so'])
72 self.assertTrue('ftype' in dt._files['pyscript'])
73
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070074
75if __name__ == '__main__':
76 cros_test_lib.main()