File: clobber_unittest.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rwxr-xr-x 4,436 bytes parent folder | download | duplicates (5)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import pathlib
import shutil
import sys
import tempfile
import textwrap
import unittest
from unittest import mock

import clobber


class TestExtractBuildCommand(unittest.TestCase):
  def setUp(self):
    self.build_ninja_file, self.build_ninja_path = tempfile.mkstemp(text=True)

  def tearDown(self):
    os.close(self.build_ninja_file)
    os.remove(self.build_ninja_path)

  def test_normal_extraction(self):
    build_ninja_file_contents = textwrap.dedent("""
        ninja_required_version = 1.7.2

        rule gn
          command = ../../buildtools/gn --root=../.. -q --regeneration gen .
          pool = console
          description = Regenerating ninja files

        build build.ninja.stamp: gn
          generator = 1
          depfile = build.ninja.d

        build build.ninja: phony build.ninja.stamp
          generator = 1

        pool build_toolchain_action_pool
          depth = 72

        pool build_toolchain_link_pool
          depth = 23

        subninja toolchain.ninja
        subninja clang_newlib_x64/toolchain.ninja
        subninja glibc_x64/toolchain.ninja
        subninja irt_x64/toolchain.ninja
        subninja nacl_bootstrap_x64/toolchain.ninja
        subninja newlib_pnacl/toolchain.ninja

        build blink_python_tests: phony obj/blink_python_tests.stamp
        build blink_tests: phony obj/blink_tests.stamp

        default all
    """)  # Based off of a standard linux build dir.
    with open(self.build_ninja_path, 'w') as f:
      f.write(build_ninja_file_contents)

    expected_build_ninja_file_contents = textwrap.dedent("""
        ninja_required_version = 1.7.2

        rule gn
          command = ../../buildtools/gn --root=../.. -q --regeneration gen .
          pool = console
          description = Regenerating ninja files

        build build.ninja.stamp: gn
          generator = 1
          depfile = build.ninja.d

        build build.ninja: phony build.ninja.stamp
          generator = 1

    """)

    self.assertEqual(clobber.extract_gn_build_commands(self.build_ninja_path),
                     expected_build_ninja_file_contents)

  def test_unexpected_format(self):
    # No "build build.ninja:" line should make it return an empty string.
    build_ninja_file_contents = textwrap.dedent("""
        ninja_required_version = 1.7.2

        rule gn
          command = ../../buildtools/gn --root=../.. -q --regeneration gen .
          pool = console
          description = Regenerating ninja files

        subninja toolchain.ninja

        build blink_python_tests: phony obj/blink_python_tests.stamp
        build blink_tests: phony obj/blink_tests.stamp

    """)
    with open(self.build_ninja_path, 'w') as f:
      f.write(build_ninja_file_contents)

    self.assertEqual(clobber.extract_gn_build_commands(self.build_ninja_path),
                     '')


class TestDelete(unittest.TestCase):
  def setUp(self):
    self.build_dir = tempfile.mkdtemp()

    pathlib.Path(os.path.join(self.build_dir, 'build.ninja')).touch()
    pathlib.Path(os.path.join(self.build_dir, 'build.ninja.d')).touch()

  def tearDown(self):
    shutil.rmtree(self.build_dir)

  def test_delete_build_dir_full(self):
    # Create a dummy file in the build dir and ensure it gets removed.
    dummy_file = os.path.join(self.build_dir, 'dummy')
    pathlib.Path(dummy_file).touch()

    clobber.delete_build_dir(self.build_dir)

    self.assertFalse(os.path.exists(dummy_file))

  def test_delete_build_dir_fail(self):
    # Make delete_dir() throw to ensure it's handled gracefully.

    with mock.patch('clobber._clean_dir', side_effect=OSError):
      with self.assertRaises(OSError):
        clobber.delete_build_dir(self.build_dir)

  @unittest.skipIf(sys.platform == 'win32', 'Symlinks are not allowed on Windows by default')
  def test_delete_build_dir_link(self):
    with tempfile.TemporaryDirectory() as tmpdir:
      # create a symlink.
      build_dir = os.path.join(tmpdir, 'link')
      os.symlink(self.build_dir, build_dir)

      # create a dummy file.
      dummy_file = os.path.join(build_dir, 'dummy')
      pathlib.Path(dummy_file).touch()
      clobber.delete_build_dir(build_dir)

      self.assertFalse(os.path.exists(dummy_file))


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