Yun Peng | 66437a0 | 2017-08-25 09:59:14 +0200 | [diff] [blame] | 1 | # Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
| 16 | import unittest |
| 17 | from src.test.py.bazel import test_base |
| 18 | |
| 19 | class DEFParserTest(test_base.TestBase): |
| 20 | |
| 21 | def createAndBuildProjectFiles(self): |
| 22 | self.ScratchFile('WORKSPACE') |
| 23 | self.ScratchFile('BUILD', ['cc_library(name="hello", srcs=["x.cc"])']) |
| 24 | self.ScratchFile('x.cc', [ |
| 25 | '#include <stdio.h>', |
| 26 | 'int hello_data;', |
| 27 | 'void hello_world() {', |
| 28 | ' printf("hello world\\n");', |
| 29 | '}', |
| 30 | ]) |
| 31 | exit_code, _, stderr = self.RunBazel(['build', '//:hello']) |
| 32 | self.AssertExitCode(exit_code, 0, stderr) |
| 33 | |
| 34 | def testParseDefFileFromObjectFile(self): |
| 35 | # Skip this test on non-Windows platforms |
| 36 | if not self.IsWindows(): |
| 37 | return |
| 38 | self.createAndBuildProjectFiles() |
| 39 | |
| 40 | exit_code, stdout, stderr = self.RunBazel(['info', 'bazel-bin']) |
| 41 | self.AssertExitCode(exit_code, 0, stderr) |
| 42 | bazel_bin = stdout[0] |
| 43 | |
| 44 | objfile = os.path.join(bazel_bin, '_objs', 'hello', 'x.o') |
| 45 | self.assertTrue(os.path.isfile(objfile)) |
| 46 | output_def = self.Path('x.def'); |
| 47 | self.RunProgram([self.Rlocation('io_bazel/third_party/def_parser/def_parser.exe'), output_def, 'my_x.dll', objfile]) |
| 48 | self.assertTrue(os.path.isfile(output_def)) |
| 49 | |
| 50 | with open(output_def, 'r') as def_file: |
| 51 | def_content = def_file.read() |
| 52 | self.assertIn('LIBRARY my_x.dll', def_content) |
| 53 | self.assertIn('hello_data', def_content) |
| 54 | self.assertIn('hello_world', def_content) |
| 55 | |
| 56 | def testParseDefFileFromObjectFileWithParamFile(self): |
| 57 | # Skip this test on non-Windows platforms |
| 58 | if not self.IsWindows(): |
| 59 | return |
| 60 | self.createAndBuildProjectFiles() |
| 61 | |
| 62 | exit_code, stdout, stderr = self.RunBazel(['info', 'bazel-bin']) |
| 63 | self.AssertExitCode(exit_code, 0, stderr) |
| 64 | bazel_bin = stdout[0] |
| 65 | |
| 66 | objfile = os.path.join(bazel_bin, '_objs', 'hello', 'x.o') |
| 67 | self.assertTrue(os.path.isfile(objfile)) |
| 68 | objfilelist = self.ScratchFile('objfilelist', [objfile]) |
| 69 | |
| 70 | output_def = self.Path('x.def'); |
| 71 | self.RunProgram([self.Rlocation('io_bazel/third_party/def_parser/def_parser.exe'), output_def, 'my_x.dll', '@' + objfilelist]) |
| 72 | self.assertTrue(os.path.isfile(output_def)) |
| 73 | |
| 74 | with open(output_def, 'r') as def_file: |
| 75 | def_content = def_file.read() |
| 76 | self.assertIn('LIBRARY my_x.dll', def_content) |
| 77 | self.assertIn('hello_data', def_content) |
| 78 | self.assertIn('hello_world', def_content) |
| 79 | |
| 80 | def testParseDefFileFromAnotherDefFile(self): |
| 81 | # Skip this test on non-Windows platforms |
| 82 | if not self.IsWindows(): |
| 83 | return |
| 84 | |
| 85 | self.createAndBuildProjectFiles() |
| 86 | |
| 87 | exit_code, stdout, stderr = self.RunBazel(['info', 'bazel-bin']) |
| 88 | self.AssertExitCode(exit_code, 0, stderr) |
| 89 | bazel_bin = stdout[0] |
| 90 | |
| 91 | objfile = os.path.join(bazel_bin, '_objs', 'hello', 'x.o') |
| 92 | self.assertTrue(os.path.isfile(objfile)) |
| 93 | output_def = self.Path('x.def'); |
| 94 | self.RunProgram([self.Rlocation('io_bazel/third_party/def_parser/def_parser.exe'), output_def, 'my_x.dll', objfile]) |
| 95 | self.assertTrue(os.path.isfile(output_def)) |
| 96 | |
| 97 | new_output_def = self.Path('new_x.def'); |
| 98 | self.RunProgram([self.Rlocation('io_bazel/third_party/def_parser/def_parser.exe'), new_output_def, 'my_x.dll', output_def]) |
| 99 | self.assertTrue(os.path.isfile(new_output_def)) |
| 100 | |
| 101 | with open(new_output_def, 'r') as def_file: |
| 102 | def_content = def_file.read() |
| 103 | self.assertIn('LIBRARY my_x.dll', def_content) |
| 104 | self.assertIn('hello_data', def_content) |
| 105 | self.assertIn('hello_world', def_content) |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | unittest.main() |