aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/pkg/make_rpm_test.py
blob: c3847359496d91aab9fec8974906a4a22e1bf9dc (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
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
149
150
151
152
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# 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.
"""Tests for make_rpm."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib
import os
import unittest

from tools.build_defs.pkg import make_rpm


@contextlib.contextmanager
def PrependPath(dirs):
  with ReplacePath(dirs + [os.environ['PATH']]):
    yield


@contextlib.contextmanager
def ReplacePath(dirs):
  original_path = os.environ['PATH']
  try:
    os.environ['PATH'] = os.pathsep.join(dirs)
    yield
  finally:
    os.environ['PATH'] = original_path


def WriteFile(filename, *contents):
  with open(filename, 'w') as text_file:
    text_file.write('\n'.join(contents))


def DirExists(dirname):
  return os.path.exists(dirname) and os.path.isdir(dirname)


def FileExists(filename):
  return os.path.exists(filename) and not os.path.isdir(filename)


def FileContents(filename):
  with open(filename, 'r') as text_file:
    return [s.strip() for s in text_file.readlines()]


class MakeRpmTest(unittest.TestCase):

  # Python 2 alias
  if not hasattr(unittest.TestCase, 'assertCountEqual'):

    def assertCountEqual(self, *args):
      return self.assertItemsEqual(*args)

  def testFindOutputFile(self):
    log = """
    Lots of data.
    Wrote: /path/to/file/here.rpm
    More data present.
    """

    result = make_rpm.FindOutputFile(log)
    self.assertEqual('/path/to/file/here.rpm', result)

  def testFindOutputFile_missing(self):
    log = """
    Lots of data.
    More data present.
    """

    result = make_rpm.FindOutputFile(log)
    self.assertEqual(None, result)

  def testCopyAndRewrite(self):
    with make_rpm.Tempdir():
      WriteFile('test.txt', 'Some: data1', 'Other: data2', 'More: data3')
      make_rpm.CopyAndRewrite('test.txt', 'out.txt', {
          'Some:': 'data1a',
          'More:': 'data3a',
      })

      self.assertTrue(FileExists('out.txt'))
      self.assertCountEqual(['Some: data1a', 'Other: data2', 'More: data3a'],
                            FileContents('out.txt'))

  def testFindRpmbuild_present(self):
    with make_rpm.Tempdir() as outer:
      dummy = os.sep.join([outer, 'rpmbuild'])
      WriteFile(dummy, 'dummy rpmbuild')
      os.chmod(dummy, 0o777)
      with PrependPath([outer]):
        path = make_rpm.FindRpmbuild()
        self.assertEqual(dummy, path)

  def testFindRpmbuild_missing(self):
    with make_rpm.Tempdir() as outer:
      with ReplacePath([outer]):
        with self.assertRaises(make_rpm.NoRpmbuildFound) as context:
          make_rpm.FindRpmbuild()
        self.assertIsNotNone(context)

  def testSetupWorkdir(self):
    with make_rpm.Tempdir() as outer:
      dummy = os.sep.join([outer, 'rpmbuild'])
      WriteFile(dummy, 'dummy rpmbuild')
      os.chmod(dummy, 0o777)

      with PrependPath([outer]):
        # Create the builder and exercise it.
        builder = make_rpm.RpmBuilder('test', '1.0', 'x86')

        # Create spec_file, test files.
        WriteFile('test.spec', 'Name: test', 'Version: 0.1',
                  'Summary: test data')
        WriteFile('file1.txt', 'Hello')
        WriteFile('file2.txt', 'Goodbye')
        builder.AddFiles(['file1.txt', 'file2.txt'])

        with make_rpm.Tempdir():
          # Call RpmBuilder.
          builder.SetupWorkdir('test.spec', outer)

          # Make sure files exist.
          self.assertTrue(DirExists('SOURCES'))
          self.assertTrue(DirExists('BUILD'))
          self.assertTrue(DirExists('TMP'))
          self.assertTrue(FileExists('test.spec'))
          self.assertCountEqual(
              ['Name: test', 'Version: 1.0', 'Summary: test data'],
              FileContents('test.spec'))
          self.assertTrue(FileExists('BUILD/file1.txt'))
          self.assertCountEqual(['Hello'], FileContents('BUILD/file1.txt'))
          self.assertTrue(FileExists('BUILD/file2.txt'))
          self.assertCountEqual(['Goodbye'], FileContents('BUILD/file2.txt'))


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