summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/tools/tests/fix_deps_test.py
blob: 6024e568471be689c062fc8d3d1d70f1e8aedb86 (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
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys
import tempfile
import unittest

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(SCRIPT_DIR)
DATA_DIR = os.path.join(SCRIPT_DIR, 'data')
CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(PARENT_DIR)))
MOCK_DIR = os.path.join(CHROME_SRC, 'third_party', 'pymock')

# For the mock library
sys.path.append(MOCK_DIR)
sys.path.append(PARENT_DIR)

import fix_deps
import mock


class TestFixDeps(unittest.TestCase):
  def setUp(self):
    self.tempfile = None

  def tearDown(self):
    if self.tempfile:
      os.remove(self.tempfile)

  def testRequiresFile(self):
    with mock.patch('sys.stderr'):
      self.assertRaises(SystemExit, fix_deps.main, [])

  def testInvalidOption(self):
    with mock.patch('sys.stderr'):
      self.assertRaises(SystemExit, fix_deps.main, ['--foo', 'bar'])

  def testMissingFile(self):
    with mock.patch('sys.stderr'):
      self.assertRaises(fix_deps.Error, fix_deps.main, ['nonexistent.file'])

  def testAddsDeps(self):
    self.tempfile = tempfile.mktemp("_sdktest")
    with open(self.tempfile, 'w') as out:
      out.write('foo.o: foo.c foo.h bar.h\n')
    fix_deps.FixupDepFile(self.tempfile)
    with open(self.tempfile) as infile:
      contents = infile.read()
    lines = contents.splitlines()
    self.assertEqual(len(lines), 5)
    self.assertTrue('foo.c:' in lines)
    self.assertTrue('foo.h:' in lines)
    self.assertTrue('bar.h:' in lines)

  def testSpacesInFilenames(self):
    self.tempfile = tempfile.mktemp("_sdktest")
    with open(self.tempfile, 'w') as out:
      out.write('foo.o: foo\\ bar.h\n')
    fix_deps.FixupDepFile(self.tempfile)
    with open(self.tempfile) as infile:
      contents = infile.read()
    lines = contents.splitlines()
    self.assertEqual(len(lines), 3)
    self.assertEqual(lines[2], 'foo\\ bar.h:')

  def testColonInFilename(self):
    self.tempfile = tempfile.mktemp("_sdktest")
    with open(self.tempfile, 'w') as out:
      out.write('foo.o: c:foo.c\\\n c:bar.h\n')
    fix_deps.FixupDepFile(self.tempfile)
    with open(self.tempfile) as infile:
      contents = infile.read()
    lines = contents.splitlines()
    self.assertEqual(len(lines), 5)
    self.assertEqual(lines[3], 'c:foo.c:')
    self.assertEqual(lines[4], 'c:bar.h:')

  def testDoubleInvoke(self):
    self.tempfile = tempfile.mktemp("_sdktest")
    with open(self.tempfile, 'w') as out:
      out.write('foo.o: foo\\ bar.h\n')
    fix_deps.FixupDepFile(self.tempfile)
    self.assertRaises(fix_deps.Error, fix_deps.FixupDepFile, self.tempfile)


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