Skip to content

Commit 6cbb141

Browse files
committed
[Dependency Scanning] Add a unittest harness to invoke the dependency scanning tool C++ library
1 parent f3d217e commit 6cbb141

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed

unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if(SWIFT_INCLUDE_TOOLS)
1313
add_subdirectory(IDE)
1414
add_subdirectory(Parse)
1515
add_subdirectory(Sema)
16+
add_subdirectory(SwiftScan)
1617
add_subdirectory(SwiftDemangle)
1718
add_subdirectory(Syntax)
1819
if(SWIFT_BUILD_SYNTAXPARSERLIB)

unittests/SwiftScan/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_swift_unittest(swiftScanTests
2+
ScanFixture.cpp
3+
ModuleDeps.cpp)
4+
5+
target_link_libraries(swiftScanTests
6+
PRIVATE
7+
SwiftScanTool)
8+
9+
target_compile_definitions(swiftScanTests PRIVATE
10+
SWIFTLIB_DIR=\"${SWIFTLIB_DIR}\")

unittests/SwiftScan/ModuleDeps.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//===---------------------- ModuleDeps.cpp --------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 "ScanFixture.h"
14+
#include "swift/Basic/Platform.h"
15+
#include "llvm/ADT/Triple.h"
16+
#include "llvm/Support/raw_ostream.h"
17+
#include "llvm/Support/Host.h"
18+
#include "llvm/Support/Path.h"
19+
20+
using namespace swift;
21+
using namespace swift::unittest;
22+
using namespace swift::dependencies;
23+
24+
static std::string createFilename(StringRef base, StringRef name) {
25+
SmallString<256> path = base;
26+
llvm::sys::path::append(path, name);
27+
return llvm::Twine(path).str();
28+
}
29+
30+
static bool emitFileWithContents(StringRef path, StringRef contents,
31+
std::string *pathOut = nullptr) {
32+
int FD;
33+
if (llvm::sys::fs::openFileForWrite(path, FD))
34+
return true;
35+
if (pathOut)
36+
*pathOut = path.str();
37+
llvm::raw_fd_ostream file(FD, /*shouldClose=*/true);
38+
file << contents;
39+
return false;
40+
}
41+
42+
static bool emitFileWithContents(StringRef base, StringRef name,
43+
StringRef contents,
44+
std::string *pathOut = nullptr) {
45+
return emitFileWithContents(createFilename(base, name), contents, pathOut);
46+
}
47+
48+
TEST_F(ScanTest, TestModuleDeps) {
49+
// Create test input file
50+
auto TestFilePath = TemporaryTestWorkspace;
51+
llvm::sys::path::append(TestFilePath, "foo.swift");
52+
std::string TestPathStr = llvm::Twine(TestFilePath).str();
53+
54+
ASSERT_FALSE(emitFileWithContents(TemporaryTestWorkspace, "foo.swift",
55+
"import A\n"));
56+
57+
// Create includes
58+
std::string IncludeDirPath = createFilename(TemporaryTestWorkspace,
59+
"include");
60+
ASSERT_FALSE(llvm::sys::fs::create_directory(IncludeDirPath));
61+
62+
std::string CHeadersDirPath = createFilename(IncludeDirPath, "CHeaders");
63+
ASSERT_FALSE(llvm::sys::fs::create_directory(CHeadersDirPath));
64+
std::string SwiftDirPath = createFilename(IncludeDirPath, "Swift");
65+
ASSERT_FALSE(llvm::sys::fs::create_directory(SwiftDirPath));
66+
67+
// Create imported module Swift interface files
68+
ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "A.swiftinterface",
69+
"// swift-interface-format-version: 1.0\n\
70+
// swift-module-flags: -module-name A\n\
71+
import Swift\n\
72+
@_exported import A\n\
73+
public func overlayFuncA() { }\n"));
74+
ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "E.swiftinterface",
75+
"// swift-interface-format-version: 1.0\n\
76+
// swift-module-flags: -module-name E\n\
77+
import Swift\n\
78+
public func funcE()\n"));
79+
ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "F.swiftinterface",
80+
"// swift-interface-format-version: 1.0\n\
81+
// swift-module-flags: -module-name\n\
82+
import Swift\n\
83+
@_exported import F\n\
84+
public func funcF() { }"));
85+
ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "G.swiftinterface",
86+
"// swift-interface-format-version: 1.0\n\
87+
// swift-module-flags: -module-name G -swift-version 5 -target x86_64-apple-macosx10.9\n\
88+
#if swift(>=5.0)\n\
89+
@_exported import G\n\
90+
import Swift\n\
91+
public func overlayFuncG() { }\n\
92+
let stringG : String = \"Build\"\n\
93+
#endif"));
94+
95+
// Create imported module C modulemap/headers
96+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "A.h",
97+
"void funcA(void);"));
98+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "B.h",
99+
"#include \"A.h\"\
100+
void funcB(void);"));
101+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "C.h",
102+
"#include \"B.h\"\n\
103+
void funcC(void);\
104+
const char* stringC() { return \"Module\"; }"));
105+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "D.h",
106+
"void funcD(void);"));
107+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "F.h",
108+
"void funcF(void);"));
109+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "G.h",
110+
"#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 110000\n\
111+
#include \"X.h\"\n\
112+
#endif\n\
113+
void funcG(void);"));
114+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "X.h",
115+
"void funcX(void);"));
116+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "Bridging.h",
117+
"#include \"BridgingOther.h\"\n\
118+
int bridging_other(void);"));
119+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "BridgingOther.h",
120+
"#include \"F.h\"\n\
121+
int bridging_other(void);"));
122+
123+
ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "module.modulemap",
124+
"module A {\n\
125+
header \"A.h\"\n\
126+
export *\n\
127+
}\n\
128+
module B {\n\
129+
header \"B.h\"\n\
130+
export *\n\
131+
}\n\
132+
module C {\n\
133+
header \"C.h\"\n\
134+
export *\n\
135+
}\n\
136+
module D {\n\
137+
header \"D.h\"\n\
138+
export *\n\
139+
}\n\
140+
module F {\n\
141+
header \"F.h\"\n\
142+
export *\n\
143+
}\n\
144+
module G {\n\
145+
header \"G.h\"\n\
146+
export *\n\
147+
}\n\
148+
module X {\n\
149+
header \"X.h\"\n\
150+
export *\n\
151+
}"));
152+
153+
// Paths to shims and stdlib
154+
llvm::SmallString<128> ShimsLibDir = StdLibDir;
155+
llvm::sys::path::append(ShimsLibDir, "shims");
156+
auto Target = llvm::Triple(llvm::sys::getDefaultTargetTriple());
157+
llvm::sys::path::append(StdLibDir, getPlatformNameForTriple(Target));
158+
159+
std::string Command = TestPathStr + " -I " + SwiftDirPath +
160+
" -I " + CHeadersDirPath +
161+
" -I " + StdLibDir.str().str() +
162+
" -I " + ShimsLibDir.str().str();
163+
std::string deps = ScannerTool.getFullDependencies(Command.c_str(),
164+
{TestPathStr.c_str()}, {});
165+
166+
// TODO: Output/verify dependency graph correctness
167+
//llvm::dbgs() << "Deps: " << deps << "\n";
168+
}

unittests/SwiftScan/ScanFixture.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-------- ScanFixture.cpp - Dependency scanning tests -*- C++ -------*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 "ScanFixture.h"
14+
#include "swift/Basic/LLVMInitialize.h"
15+
#include "llvm/Support/FileSystem.h"
16+
#include "gtest/gtest.h"
17+
18+
using namespace swift;
19+
using namespace swift::unittest;
20+
using namespace swift::dependencies;
21+
22+
ScanTest::ScanTest()
23+
: ScannerTool(),
24+
StdLibDir(SWIFTLIB_DIR) {
25+
INITIALIZE_LLVM();
26+
// Create a temporary filesystem workspace for this test.
27+
llvm::sys::fs::createUniqueDirectory(
28+
"ScanTest.Workspace", TemporaryTestWorkspace);
29+
}
30+
31+
ScanTest::~ScanTest() {
32+
llvm::sys::fs::remove_directories(TemporaryTestWorkspace);
33+
}

unittests/SwiftScan/ScanFixture.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-------- ScanFixture.h - Dependency scanning tests -*- C++ ---------*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 "swift/SwiftScan/DependencyScanningTool.h"
14+
#include "gtest/gtest.h"
15+
#include <string>
16+
17+
using namespace swift::dependencies;
18+
19+
namespace swift {
20+
namespace unittest {
21+
22+
class ScanTest : public ::testing::Test {
23+
public:
24+
ScanTest();
25+
~ScanTest();
26+
27+
protected:
28+
// The tool used to execute tests' scanning queries
29+
DependencyScanningTool ScannerTool;
30+
31+
// Test workspace directory
32+
llvm::SmallString<256> TemporaryTestWorkspace;
33+
34+
// Path to where the Swift standard library can be found
35+
llvm::SmallString<128> StdLibDir;
36+
};
37+
38+
39+
} // end namespace unittest
40+
} // end namespace swift

0 commit comments

Comments
 (0)