blob: 6e9a6cae70e0c4a5c35968500074b147eef1ac4a [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Alex Deymo3cfb9cd2014-08-18 15:56:35 -07002# 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 Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070010import os
Mike Frysinger03b983f2020-02-21 02:31:49 -050011import sys
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070012
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
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070018pytestmark = cros_test_lib.pytestmark_inside_only
19
Mike Frysinger03b983f2020-02-21 02:31:49 -050020
21assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070024# Allow access private members for testing:
Mike Frysinger27e21b72018-07-12 14:20:21 -040025# pylint: disable=protected-access
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070026
27
Mike Frysinger68b65042014-12-07 01:21:22 -050028class MainTest(cros_test_lib.OutputTestCase):
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070029 """Tests for the main() function."""
30
31 def testHelp(self):
32 """Test that --help is functioning."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050033 argv = ['--help']
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070034
35 with self.OutputCapturer() as output:
36 # Running with --help should exit with code==0.
37 self.AssertFuncSystemExitZero(dep_tracker.main, argv)
38
39 # Verify that a message beginning with "usage: " was printed.
40 stdout = output.GetStdout()
41 self.assertTrue(stdout.startswith('usage: '))
42
43
44class DepTrackerTest(cros_test_lib.TempDirTestCase):
45 """Tests for the DepTracker() class."""
46
47 def testSimpleDep(self):
48 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
49 ['func_a', 'func_b', 'func_c'])
50 unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'),
51 undefined_symbols=['func_b'],
52 used_libs=['abc'],
53 executable=True)
54 dt = dep_tracker.DepTracker(self.tempdir)
55 dt.Init()
56 dt.ComputeELFFileDeps()
57
Mike Frysinger2d589a12019-08-25 14:15:12 -040058 self.assertEqual(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070059
Alex Deymo365b10c2014-08-25 13:14:28 -070060 def testFiletypeSet(self):
61 """Tests that the 'ftype' member is set for ELF files first."""
62 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
63 ['func_a', 'func_b', 'func_c'])
64 osutils.WriteFile(os.path.join(self.tempdir, 'pyscript'),
Mike Frysinger80de5012019-08-01 14:10:53 -040065 '#!/usr/bin/python\nimport sys\nsys.exit(42)\n')
Alex Deymo365b10c2014-08-25 13:14:28 -070066 dt = dep_tracker.DepTracker(self.tempdir)
67 dt.Init()
68
69 # ComputeELFFileDeps() should compute the file type of ELF files so we
70 # don't need to parse them again.
71 dt.ComputeELFFileDeps()
72 self.assertTrue('ftype' in dt._files['libabc.so'])
73 self.assertFalse('ftype' in dt._files['pyscript'])
74
75 # ComputeFileTypes() shold compute the file type of every file.
76 dt.ComputeFileTypes()
77 self.assertTrue('ftype' in dt._files['libabc.so'])
78 self.assertTrue('ftype' in dt._files['pyscript'])