aboutsummaryrefslogtreecommitdiff
path: root/scripts/difftool/difftool_test.py
blob: 2baa6a777a0cd66f274b2dcfcf67cd0fe795279f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for difftool.py."""

import os
import pathlib
import unittest
import clangcompile
import difftool


def get_path(name):
  return os.path.join(os.getenv("TEST_TMPDIR"), name)


def create_file(name, content):
  path = get_path(name)
  with open(path, "w") as f:
    f.write(content)
  return pathlib.Path(path)


def _substring_in_list(s, slist):
  for elem in slist:
    if s in elem:
      return True
  return False


class DifftoolTest(unittest.TestCase):

  def assertNotInErrors(self, expected, errorlist):
    if _substring_in_list(expected, errorlist):
      self.fail("{!r} found in errors: {!r}".format(expected, errorlist))

  def assertInErrors(self, expected, errorlist):
    if not _substring_in_list(expected, errorlist):
      self.fail("{!r} not found in errors: {!r}".format(expected, errorlist))

  def test_file_differences_not_exist(self):
    obj_file = create_file("foo.o", "object contents")

    diffs = difftool.file_differences(pathlib.Path("doesntexist.o"),
                                      obj_file)
    self.assertEqual(["doesntexist.o does not exist"], diffs)

  def test_file_differences_different_types(self):
    obj_file = create_file("foo.o", "object contents")
    obj_file_two = create_file("foo2.o", "object contents two")
    txt_file = create_file("foo3.txt", "other")
    so_file = create_file("bar.so", "shared lib contents")

    diffs = difftool.file_differences(obj_file, so_file)
    self.assertInErrors("file types differ", diffs)

    diffs = difftool.file_differences(obj_file, txt_file)
    self.assertInErrors("file types differ", diffs)

    diffs = difftool.file_differences(so_file, obj_file)
    self.assertInErrors("file types differ", diffs)

    diffs = difftool.file_differences(obj_file, obj_file_two)
    self.assertNotInErrors("file types differ", diffs)

  def test_object_contents_differ(self):
    obj_file = create_file("foo.o", "object contents\none\n")
    obj_file_two = create_file("foo2.o", "object contents\ntwo\n")

    diffs = difftool.file_differences(obj_file, obj_file_two)
    self.assertNotInErrors("object_contents", diffs)
    self.assertInErrors("one", diffs)
    self.assertInErrors("two", diffs)

  def test_soong_clang_compile_info(self):
    fake_cmd = ("PWD=/proc/self/cwd prebuilts/clang -c -Wall -Wno-unused " +
                "foo.cpp -Iframeworks/av/include -Dsomedefine " +
                "-misc_flag misc_arg " +
                "-o foo.o # comment")
    info = difftool.rich_command_info(fake_cmd)
    self.assertIsInstance(info, clangcompile.ClangCompileInfo)
    self.assertEqual([("I", "frameworks/av/include")], info.i_includes)
    self.assertEqual(["-Dsomedefine"], info.defines)
    self.assertEqual(["-Wall", "-Wno-unused"], info.warnings)
    self.assertEqual(["-c", ("misc_flag", "misc_arg")], info.misc_flags)
    self.assertEqual(["foo.cpp", ("o", "foo.o")], info.file_flags)

  def test_bazel_clang_compile_info(self):
    fake_cmd = ("cd out/bazel/execroot && rm -f foo.o &&  " +
                "prebuilts/clang -MD -MF bazel-out/foo.d " +
                "-iquote . -iquote bazel-out/foo/bin " +
                "-I frameworks/av/include " +
                "-I bazel-out/frameworks/av/include/bin " +
                " -Dsomedefine " +
                "-misc_flag misc_arg " +
                "-Werror=int-conversion " +
                "-Wno-reserved-id-macro "
                "-o foo.o # comment")
    info = difftool.rich_command_info(fake_cmd)
    self.assertIsInstance(info, clangcompile.ClangCompileInfo)
    self.assertEqual([("iquote", ".")], info.iquote_includes)
    self.assertEqual([("I", "frameworks/av/include")], info.i_includes)
    self.assertEqual(["-Dsomedefine"], info.defines)
    self.assertEqual(["-Werror=int-conversion", "-Wno-reserved-id-macro"],
                     info.warnings)
    self.assertEqual(["-MD", ("misc_flag", "misc_arg")], info.misc_flags)
    self.assertEqual([("MF", "bazel-out/foo.d"), ("o", "foo.o")], info.file_flags)


if __name__ == "__main__":
  unittest.main()