blob: 493ac2a52ed6d742381df3ad87790301e96e2a51 [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
11
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070012from chromite.lib import cros_test_lib
Alex Deymo365b10c2014-08-25 13:14:28 -070013from chromite.lib import osutils
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070014from chromite.lib import unittest_lib
15from chromite.scripts import dep_tracker
16
17# Allow access private members for testing:
Mike Frysinger27e21b72018-07-12 14:20:21 -040018# pylint: disable=protected-access
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070019
20
Mike Frysinger68b65042014-12-07 01:21:22 -050021class MainTest(cros_test_lib.OutputTestCase):
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070022 """Tests for the main() function."""
23
24 def testHelp(self):
25 """Test that --help is functioning."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050026 argv = ['--help']
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070027
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
37class 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 Frysinger2d589a12019-08-25 14:15:12 -040051 self.assertEqual(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
Alex Deymo3cfb9cd2014-08-18 15:56:35 -070052
Alex Deymo365b10c2014-08-25 13:14:28 -070053 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 Frysinger80de5012019-08-01 14:10:53 -040058 '#!/usr/bin/python\nimport sys\nsys.exit(42)\n')
Alex Deymo365b10c2014-08-25 13:14:28 -070059 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'])