Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 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 Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 10 | import os |
| 11 | |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 12 | from chromite.lib import cros_test_lib |
Alex Deymo | 365b10c | 2014-08-25 13:14:28 -0700 | [diff] [blame] | 13 | from chromite.lib import osutils |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 14 | from chromite.lib import unittest_lib |
| 15 | from chromite.scripts import dep_tracker |
| 16 | |
| 17 | # Allow access private members for testing: |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 18 | # pylint: disable=protected-access |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 19 | |
| 20 | |
Mike Frysinger | 68b6504 | 2014-12-07 01:21:22 -0500 | [diff] [blame] | 21 | class MainTest(cros_test_lib.OutputTestCase): |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 22 | """Tests for the main() function.""" |
| 23 | |
| 24 | def testHelp(self): |
| 25 | """Test that --help is functioning.""" |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 26 | argv = ['--help'] |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 27 | |
| 28 | with self.OutputCapturer() as output: |
| 29 | # Running with --help should exit with code==0. |
| 30 | self.AssertFuncSystemExitZero(dep_tracker.main, argv) |
| 31 | |
| 32 | # Verify that a message beginning with "usage: " was printed. |
| 33 | stdout = output.GetStdout() |
| 34 | self.assertTrue(stdout.startswith('usage: ')) |
| 35 | |
| 36 | |
| 37 | class DepTrackerTest(cros_test_lib.TempDirTestCase): |
| 38 | """Tests for the DepTracker() class.""" |
| 39 | |
| 40 | def testSimpleDep(self): |
| 41 | unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'), |
| 42 | ['func_a', 'func_b', 'func_c']) |
| 43 | unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'), |
| 44 | undefined_symbols=['func_b'], |
| 45 | used_libs=['abc'], |
| 46 | executable=True) |
| 47 | dt = dep_tracker.DepTracker(self.tempdir) |
| 48 | dt.Init() |
| 49 | dt.ComputeELFFileDeps() |
| 50 | |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 51 | self.assertEqual(sorted(dt._files.keys()), ['abc_main', 'libabc.so']) |
Alex Deymo | 3cfb9cd | 2014-08-18 15:56:35 -0700 | [diff] [blame] | 52 | |
Alex Deymo | 365b10c | 2014-08-25 13:14:28 -0700 | [diff] [blame] | 53 | def testFiletypeSet(self): |
| 54 | """Tests that the 'ftype' member is set for ELF files first.""" |
| 55 | unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'), |
| 56 | ['func_a', 'func_b', 'func_c']) |
| 57 | osutils.WriteFile(os.path.join(self.tempdir, 'pyscript'), |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame] | 58 | '#!/usr/bin/python\nimport sys\nsys.exit(42)\n') |
Alex Deymo | 365b10c | 2014-08-25 13:14:28 -0700 | [diff] [blame] | 59 | dt = dep_tracker.DepTracker(self.tempdir) |
| 60 | dt.Init() |
| 61 | |
| 62 | # ComputeELFFileDeps() should compute the file type of ELF files so we |
| 63 | # don't need to parse them again. |
| 64 | dt.ComputeELFFileDeps() |
| 65 | self.assertTrue('ftype' in dt._files['libabc.so']) |
| 66 | self.assertFalse('ftype' in dt._files['pyscript']) |
| 67 | |
| 68 | # ComputeFileTypes() shold compute the file type of every file. |
| 69 | dt.ComputeFileTypes() |
| 70 | self.assertTrue('ftype' in dt._files['libabc.so']) |
| 71 | self.assertTrue('ftype' in dt._files['pyscript']) |