|
| 1 | +//===------- BlocklistTest.cpp --------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "gtest/gtest.h" |
| 14 | +#include "swift/AST/SearchPathOptions.h" |
| 15 | +#include "swift/Basic/Defer.h" |
| 16 | +#include "swift/Basic/BlockList.h" |
| 17 | + |
| 18 | +using namespace swift; |
| 19 | + |
| 20 | +static std::string createFilename(StringRef base, StringRef name) { |
| 21 | + SmallString<256> path = base; |
| 22 | + llvm::sys::path::append(path, name); |
| 23 | + return llvm::Twine(path).str(); |
| 24 | +} |
| 25 | + |
| 26 | +static bool emitFileWithContents(StringRef path, StringRef contents, |
| 27 | + std::string *pathOut = nullptr) { |
| 28 | + int FD; |
| 29 | + if (llvm::sys::fs::openFileForWrite(path, FD)) |
| 30 | + return true; |
| 31 | + if (pathOut) |
| 32 | + *pathOut = path.str(); |
| 33 | + llvm::raw_fd_ostream file(FD, /*shouldClose=*/true); |
| 34 | + file << contents; |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +static bool emitFileWithContents(StringRef base, StringRef name, |
| 39 | + StringRef contents, |
| 40 | + std::string *pathOut = nullptr) { |
| 41 | + return emitFileWithContents(createFilename(base, name), contents, pathOut); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(BlocklistTest, testYamlParsing) { |
| 45 | + SmallString<256> temp; |
| 46 | + ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory( |
| 47 | + "BlocklistTest.testYamlParsing", temp)); |
| 48 | + SWIFT_DEFER { llvm::sys::fs::remove_directories(temp); }; |
| 49 | + BlockListStore store; |
| 50 | + std::string path1, path2; |
| 51 | + ASSERT_FALSE(emitFileWithContents(temp, "block1.yaml", |
| 52 | + "---\n" |
| 53 | + "ShouldUseBinaryModule:\n" |
| 54 | + " ModuleName:\n" |
| 55 | + " - M1 #rdar12345\n" |
| 56 | + " - M2 #rdar12345\n" |
| 57 | + " - M3\n" |
| 58 | + " - M4\n" |
| 59 | + " ProjectName:\n" |
| 60 | + " - P1\n" |
| 61 | + " - P2\n" |
| 62 | + " - P3 #rdar12344\n" |
| 63 | + " - P4\n" |
| 64 | + "---\n" |
| 65 | + "ShouldUseTextualModule:\n" |
| 66 | + " ModuleName:\n" |
| 67 | + " - M1_2 #rdar12345\n" |
| 68 | + " - M2_2 #rdar12345\n" |
| 69 | + " - M3_2\n" |
| 70 | + " - M4_2\n" |
| 71 | + " ProjectName:\n" |
| 72 | + " - P1_2\n" |
| 73 | + " - P2_2\n" |
| 74 | + " - P3_2 #rdar12344\n" |
| 75 | + " - P4_2\n", |
| 76 | + &path1)); |
| 77 | + ASSERT_FALSE(emitFileWithContents(temp, "block2.yml", |
| 78 | + "---\n" |
| 79 | + "ShouldUseBinaryModule:\n" |
| 80 | + " ModuleName:\n" |
| 81 | + " - M1_block2 #rdar12345\n" |
| 82 | + " ProjectName:\n" |
| 83 | + " - P1_block2\n" |
| 84 | + "---\n" |
| 85 | + "ShouldUseTextualModule:\n" |
| 86 | + " ModuleName:\n" |
| 87 | + " - M1_2_block2 #rdar12345\n" |
| 88 | + " ProjectName:\n" |
| 89 | + " - P1_2_block2\n", |
| 90 | + &path2)); |
| 91 | + store.addConfigureFilePath(path1); |
| 92 | + store.addConfigureFilePath(path2); |
| 93 | + ASSERT_TRUE(store.hasBlockListAction("M1", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule)); |
| 94 | + ASSERT_TRUE(store.hasBlockListAction("M2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule)); |
| 95 | + ASSERT_TRUE(store.hasBlockListAction("P1", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule)); |
| 96 | + ASSERT_TRUE(store.hasBlockListAction("P2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule)); |
| 97 | + |
| 98 | + ASSERT_TRUE(store.hasBlockListAction("M1_2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule)); |
| 99 | + ASSERT_TRUE(store.hasBlockListAction("M2_2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule)); |
| 100 | + ASSERT_TRUE(store.hasBlockListAction("P1_2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule)); |
| 101 | + ASSERT_TRUE(store.hasBlockListAction("P2_2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule)); |
| 102 | + |
| 103 | + ASSERT_FALSE(store.hasBlockListAction("P1", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule)); |
| 104 | + ASSERT_FALSE(store.hasBlockListAction("P2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule)); |
| 105 | + ASSERT_FALSE(store.hasBlockListAction("M1", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule)); |
| 106 | + ASSERT_FALSE(store.hasBlockListAction("M2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule)); |
| 107 | + |
| 108 | + ASSERT_TRUE(store.hasBlockListAction("M1_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule)); |
| 109 | + ASSERT_TRUE(store.hasBlockListAction("P1_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule)); |
| 110 | + ASSERT_TRUE(store.hasBlockListAction("M1_2_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule)); |
| 111 | + ASSERT_TRUE(store.hasBlockListAction("P1_2_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule)); |
| 112 | + |
| 113 | + ASSERT_FALSE(store.hasBlockListAction("M1_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule)); |
| 114 | + ASSERT_FALSE(store.hasBlockListAction("P1_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule)); |
| 115 | + ASSERT_FALSE(store.hasBlockListAction("M1_2_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule)); |
| 116 | + ASSERT_FALSE(store.hasBlockListAction("P1_2_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule)); |
| 117 | +} |
0 commit comments