Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | import os |
| 8 | |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 9 | from chromite.lib import cros_test_lib |
Alex Deymo | 365b10c | 2014-08-25 13:14:28 -0700 | [diff] [blame] | 10 | from chromite.lib import osutils |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 11 | from chromite.lib import unittest_lib |
| 12 | from chromite.scripts import dep_tracker |
| 13 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 14 | |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 15 | pytestmark = cros_test_lib.pytestmark_inside_only |
| 16 | |
Mike Frysinger | 03b983f | 2020-02-21 02:31:49 -0500 | [diff] [blame] | 17 | |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 18 | # Allow access private members for testing: |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 19 | # pylint: disable=protected-access |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 20 | |
| 21 | |
Mike Frysinger | 68b6504 | 2014-12-07 01:21:22 -0500 | [diff] [blame] | 22 | class MainTest(cros_test_lib.OutputTestCase): |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 23 | """Tests for the main() function.""" |
| 24 | |
| 25 | def testHelp(self): |
| 26 | """Test that --help is functioning.""" |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 27 | argv = ['--help'] |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 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 | |
| 38 | class 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 Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 52 | self.assertEqual(sorted(dt._files.keys()), ['abc_main', 'libabc.so']) |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 53 | |
Alex Deymo | 365b10c | 2014-08-25 13:14:28 -0700 | [diff] [blame] | 54 | 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 Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame] | 59 | '#!/usr/bin/python\nimport sys\nsys.exit(42)\n') |
Alex Deymo | 365b10c | 2014-08-25 13:14:28 -0700 | [diff] [blame] | 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']) |