blob: c2b574de26a9315e11508a88f877357ffc181298 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2014 The ChromiumOS Authors
Alex Deymo3cfb9cd2014-08-18 15:56:35 -07002# 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 Klein1699fab2022-09-08 08:46:06 -060023 """Tests for the main() function."""
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 def testHelp(self):
26 """Test that --help is functioning."""
27 argv = ["--help"]
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 with self.OutputCapturer() as output:
30 # Running with --help should exit with code==0.
31 self.AssertFuncSystemExitZero(dep_tracker.main, argv)
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070032
Alex Klein1699fab2022-09-08 08:46:06 -060033 # Verify that a message beginning with "usage: " was printed.
34 stdout = output.GetStdout()
35 self.assertTrue(stdout.startswith("usage: "))
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070036
37
38class DepTrackerTest(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060039 """Tests for the DepTracker() class."""
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070040
Alex Klein1699fab2022-09-08 08:46:06 -060041 def testSimpleDep(self):
42 unittest_lib.BuildELF(
43 os.path.join(self.tempdir, "libabc.so"),
44 ["func_a", "func_b", "func_c"],
45 )
46 unittest_lib.BuildELF(
47 os.path.join(self.tempdir, "abc_main"),
48 undefined_symbols=["func_b"],
49 used_libs=["abc"],
50 executable=True,
51 )
52 dt = dep_tracker.DepTracker(self.tempdir)
53 dt.Init()
54 dt.ComputeELFFileDeps()
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070055
Alex Klein1699fab2022-09-08 08:46:06 -060056 self.assertEqual(sorted(dt._files.keys()), ["abc_main", "libabc.so"])
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070057
Alex Klein1699fab2022-09-08 08:46:06 -060058 def testFiletypeSet(self):
59 """Tests that the 'ftype' member is set for ELF files first."""
60 unittest_lib.BuildELF(
61 os.path.join(self.tempdir, "libabc.so"),
62 ["func_a", "func_b", "func_c"],
63 )
64 osutils.WriteFile(
65 os.path.join(self.tempdir, "pyscript"),
66 "#!/usr/bin/python\nimport sys\nsys.exit(42)\n",
67 )
68 dt = dep_tracker.DepTracker(self.tempdir)
69 dt.Init()
Alex Deymo365b10c2014-08-25 13:14:28 -070070
Alex Klein1699fab2022-09-08 08:46:06 -060071 # ComputeELFFileDeps() should compute the file type of ELF files so we
72 # don't need to parse them again.
73 dt.ComputeELFFileDeps()
74 self.assertTrue("ftype" in dt._files["libabc.so"])
75 self.assertFalse("ftype" in dt._files["pyscript"])
Alex Deymo365b10c2014-08-25 13:14:28 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 # ComputeFileTypes() shold compute the file type of every file.
78 dt.ComputeFileTypes()
79 self.assertTrue("ftype" in dt._files["libabc.so"])
80 self.assertTrue("ftype" in dt._files["pyscript"])