blob: a6c7e9b4d2fe65e20c1da5dd2847519efbdbe253 [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
Mike Frysinger03b983f2020-02-21 02:31:49 -050018
19assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
20
21
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070022# Allow access private members for testing:
Mike Frysinger27e21b72018-07-12 14:20:21 -040023# pylint: disable=protected-access
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070024
25
Mike Frysinger68b65042014-12-07 01:21:22 -050026class MainTest(cros_test_lib.OutputTestCase):
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070027 """Tests for the main() function."""
28
29 def testHelp(self):
30 """Test that --help is functioning."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050031 argv = ['--help']
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070032
33 with self.OutputCapturer() as output:
34 # Running with --help should exit with code==0.
35 self.AssertFuncSystemExitZero(dep_tracker.main, argv)
36
37 # Verify that a message beginning with "usage: " was printed.
38 stdout = output.GetStdout()
39 self.assertTrue(stdout.startswith('usage: '))
40
41
42class DepTrackerTest(cros_test_lib.TempDirTestCase):
43 """Tests for the DepTracker() class."""
44
45 def testSimpleDep(self):
46 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
47 ['func_a', 'func_b', 'func_c'])
48 unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'),
49 undefined_symbols=['func_b'],
50 used_libs=['abc'],
51 executable=True)
52 dt = dep_tracker.DepTracker(self.tempdir)
53 dt.Init()
54 dt.ComputeELFFileDeps()
55
Mike Frysinger2d589a12019-08-25 14:15:12 -040056 self.assertEqual(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070057
Alex Deymo365b10c2014-08-25 13:14:28 -070058 def testFiletypeSet(self):
59 """Tests that the 'ftype' member is set for ELF files first."""
60 unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
61 ['func_a', 'func_b', 'func_c'])
62 osutils.WriteFile(os.path.join(self.tempdir, 'pyscript'),
Mike Frysinger80de5012019-08-01 14:10:53 -040063 '#!/usr/bin/python\nimport sys\nsys.exit(42)\n')
Alex Deymo365b10c2014-08-25 13:14:28 -070064 dt = dep_tracker.DepTracker(self.tempdir)
65 dt.Init()
66
67 # ComputeELFFileDeps() should compute the file type of ELF files so we
68 # don't need to parse them again.
69 dt.ComputeELFFileDeps()
70 self.assertTrue('ftype' in dt._files['libabc.so'])
71 self.assertFalse('ftype' in dt._files['pyscript'])
72
73 # ComputeFileTypes() shold compute the file type of every file.
74 dt.ComputeFileTypes()
75 self.assertTrue('ftype' in dt._files['libabc.so'])
76 self.assertTrue('ftype' in dt._files['pyscript'])