Skip to content

Commit 4eae21c

Browse files
committed
Change file generation in the DependencyScan.ModuleDeps unittest to hopefully work better on windows.
1 parent cc400bf commit 4eae21c

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

unittests/DependencyScan/ModuleDeps.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212

1313
#include "ScanFixture.h"
1414
#include "swift/Basic/Platform.h"
15+
#include "swift/Basic/Defer.h"
1516
#include "llvm/ADT/Triple.h"
1617
#include "llvm/Support/Host.h"
1718
#include "llvm/Support/Path.h"
1819
#include "llvm/Support/raw_ostream.h"
20+
#include "gtest/gtest.h"
21+
#include <string>
1922

2023
using namespace swift;
2124
using namespace swift::unittest;
@@ -46,19 +49,18 @@ static bool emitFileWithContents(StringRef base, StringRef name,
4649
}
4750

4851
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();
52+
SmallString<256> tempDir;
53+
ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("ScanTest.TestModuleDeps", tempDir));
54+
SWIFT_DEFER { llvm::sys::fs::remove_directories(tempDir); };
5355

54-
ASSERT_FALSE(
55-
emitFileWithContents(TemporaryTestWorkspace, "foo.swift", "import A\n"));
56+
// Create test input file
57+
std::string TestPathStr = createFilename(tempDir, "foo.swift");
58+
ASSERT_FALSE(emitFileWithContents(tempDir, "foo.swift", "import A\n"));
59+
llvm::dbgs() << "Input File: " << TestPathStr << "\n";
5660

5761
// Create includes
58-
std::string IncludeDirPath =
59-
createFilename(TemporaryTestWorkspace, "include");
62+
std::string IncludeDirPath = createFilename(tempDir, "include");
6063
ASSERT_FALSE(llvm::sys::fs::create_directory(IncludeDirPath));
61-
6264
std::string CHeadersDirPath = createFilename(IncludeDirPath, "CHeaders");
6365
ASSERT_FALSE(llvm::sys::fs::create_directory(CHeadersDirPath));
6466
std::string SwiftDirPath = createFilename(IncludeDirPath, "Swift");
@@ -155,10 +157,27 @@ export *\n\
155157
auto Target = llvm::Triple(llvm::sys::getDefaultTargetTriple());
156158
llvm::sys::path::append(StdLibDir, getPlatformNameForTriple(Target));
157159

158-
std::string Command = TestPathStr + " -I " + SwiftDirPath + " -I " +
159-
CHeadersDirPath + " -I " + StdLibDir.str().str() +
160-
" -I " + ShimsLibDir.str().str();
161-
auto DependenciesOrErr = ScannerTool.getDependencies(Command.c_str(), {});
160+
std::vector<std::string> CommandStrArr = {
161+
std::string("'") + TestPathStr + std::string("'"),
162+
std::string("-I ") + std::string("'") + SwiftDirPath + std::string("'"),
163+
std::string("-I ") + std::string("'") + CHeadersDirPath + std::string("'"),
164+
std::string("-I ") + std::string("'") + StdLibDir.str().str() + std::string("'"),
165+
std::string("-I ") + std::string("'") + ShimsLibDir.str().str() + std::string("'")
166+
};
167+
168+
// On Windows we need to add an extra escape for path separator characters because otherwise
169+
// the command line tokenizer will treat them as escape characters.
170+
for (size_t i = 0; i < CommandStrArr.size(); ++i) {
171+
std::replace(CommandStrArr[i].begin(), CommandStrArr[i].end(), '\\', '/');
172+
}
173+
174+
std::vector<const char*> Command;
175+
llvm::dbgs() << "Compiler Command: \n";
176+
for (auto &command : CommandStrArr) {
177+
Command.push_back(command.c_str());
178+
llvm::dbgs() << command.c_str() << "\n";
179+
}
180+
auto DependenciesOrErr = ScannerTool.getDependencies(Command, {});
162181
ASSERT_FALSE(DependenciesOrErr.getError());
163182
auto Dependencies = DependenciesOrErr.get();
164183
// TODO: Output/verify dependency graph correctness

0 commit comments

Comments
 (0)