blob: 05a225620f201cb262f9227903706b54716ee455 [file] [log] [blame]
Alex Deymo3cfb9cd2014-08-18 15:56:35 -07001# Copyright 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for dep_tracker.py."""
6
7import os
8
Alex Deymo3cfb9cd2014-08-18 15:56:35 -07009from chromite.lib import cros_test_lib
Alex Deymo365b10c2014-08-25 13:14:28 -070010from chromite.lib import osutils
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070011from chromite.lib import unittest_lib
12from chromite.scripts import dep_tracker
13
Mike Frysinger807d8282022-04-28 22:45:17 -040014
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070015pytestmark = cros_test_lib.pytestmark_inside_only
16
Mike Frysinger03b983f2020-02-21 02:31:49 -050017
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070018# Allow access private members for testing:
Mike Frysinger27e21b72018-07-12 14:20:21 -040019# pylint: disable=protected-access
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070020
21
Mike Frysinger68b65042014-12-07 01:21:22 -050022class MainTest(cros_test_lib.OutputTestCase):
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070023 """Tests for the main() function."""
24
25 def testHelp(self):
26 """Test that --help is functioning."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050027 argv = ['--help']
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070028
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
Mike Frysinger2d589a12019-08-25 14:15:12 -040052 self.assertEqual(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070053
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'),
Mike Frysinger80de5012019-08-01 14:10:53 -040059 '#!/usr/bin/python\nimport sys\nsys.exit(42)\n')
Alex Deymo365b10c2014-08-25 13:14:28 -070060 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'])