blob: 9a79a41ac93bddff1ca88ba8925646bcfd5047ff [file] [log] [blame]
# Copyright 2014 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit tests for dep_tracker.py."""
import os
import pytest
from chromite.lib import cros_test_lib
from chromite.lib import osutils
from chromite.lib import unittest_lib
from chromite.scripts import dep_tracker
pytestmark = pytest.mark.inside_only
# Allow access private members for testing:
# pylint: disable=protected-access
class MainTest(cros_test_lib.TestCase):
"""Tests for the main() function."""
@pytest.mark.usefixtures("legacy_capture_output")
def testHelp(self) -> None:
"""Test that --help is functioning."""
argv = ["--help"]
# Running with --help should exit with code==0.
with pytest.raises(SystemExit) as e:
dep_tracker.main(argv)
assert e.value.code == 0
# Verify that a message beginning with "usage: " was printed.
stdout = self.capfd.readouterr().out
self.assertTrue(stdout.startswith("usage: "))
class DepTrackerTest(cros_test_lib.TempDirTestCase):
"""Tests for the DepTracker() class."""
def testSimpleDep(self) -> None:
unittest_lib.BuildELF(
os.path.join(self.tempdir, "libabc.so"),
["func_a", "func_b", "func_c"],
)
unittest_lib.BuildELF(
os.path.join(self.tempdir, "abc_main"),
undefined_symbols=["func_b"],
used_libs=["abc"],
executable=True,
)
dt = dep_tracker.DepTracker(self.tempdir)
dt.Init()
dt.ComputeELFFileDeps()
self.assertEqual(sorted(dt._files.keys()), ["abc_main", "libabc.so"])
def testFiletypeSet(self) -> None:
"""Tests that the 'ftype' member is set for ELF files first."""
unittest_lib.BuildELF(
os.path.join(self.tempdir, "libabc.so"),
["func_a", "func_b", "func_c"],
)
osutils.WriteFile(
os.path.join(self.tempdir, "pyscript"),
"#!/usr/bin/python\nimport sys\nsys.exit(42)\n",
)
dt = dep_tracker.DepTracker(self.tempdir)
dt.Init()
# ComputeELFFileDeps() should compute the file type of ELF files so we
# don't need to parse them again.
dt.ComputeELFFileDeps()
self.assertTrue("ftype" in dt._files["libabc.so"])
self.assertFalse("ftype" in dt._files["pyscript"])
# ComputeFileTypes() shold compute the file type of every file.
dt.ComputeFileTypes()
self.assertTrue("ftype" in dt._files["libabc.so"])
self.assertTrue("ftype" in dt._files["pyscript"])