1515#include " llvm/ADT/StringExtras.h"
1616#include " llvm/ADT/StringRef.h"
1717#include " llvm/Support/Path.h"
18+ #include " llvm/Support/Signals.h"
19+ #include " llvm/Support/ToolOutputFile.h"
1820#include " llvm/Support/YAMLParser.h"
1921#include " llvm/Support/YAMLTraits.h"
2022#include " llvm/Support/raw_ostream.h"
2123#include " gtest/gtest.h"
24+ #include < cstdlib>
2225#include < random>
2326#include < string>
27+ #include < system_error>
2428
2529using namespace swift ;
2630using namespace swift ::diag;
@@ -36,16 +40,28 @@ static constexpr const char *const diagnosticMessages[] = {
3640#include " swift/AST/DiagnosticsAll.def"
3741};
3842
39- static std::string getDefaultLocalizationPath () {
43+ static std::string getMainExecutablePath () {
4044 std::string libPath = llvm::sys::path::parent_path (SWIFTLIB_DIR);
41- llvm::SmallString<128 > DefaultDiagnosticMessagesDir (libPath);
42- llvm::sys::path::remove_filename (DefaultDiagnosticMessagesDir); // Remove /lib
43- llvm::sys::path::remove_filename (DefaultDiagnosticMessagesDir); // Remove /.
45+ llvm::SmallString<128 > MainExecutablePath (libPath);
46+ llvm::sys::path::remove_filename (MainExecutablePath); // Remove /lib
47+ llvm::sys::path::remove_filename (MainExecutablePath); // Remove /.
48+ return std::string (MainExecutablePath.str ());
49+ }
50+
51+ static std::string getDefaultLocalizationPath () {
52+ llvm::SmallString<128 > DefaultDiagnosticMessagesDir (getMainExecutablePath ());
4453 llvm::sys::path::append (DefaultDiagnosticMessagesDir, " share" , " swift" ,
4554 " diagnostics" );
4655 return std::string (DefaultDiagnosticMessagesDir.str ());
4756}
4857
58+ static std::string getDefToYAMLConverterPath () {
59+ llvm::SmallString<128 > defYAMLConverter (getMainExecutablePath ());
60+ llvm::sys::path::append (defYAMLConverter, " bin" ,
61+ " swift-def-to-yaml-converter" );
62+ return std::string (defYAMLConverter.str ());
63+ }
64+
4965// / Random number in [0,n)
5066unsigned randNum (unsigned n) { return unsigned (rand ()) % n; }
5167
@@ -60,22 +76,37 @@ TEST(DefToYAMLConverterTest, missingLocalizationFiles) {
6076}
6177
6278TEST (DefToYAMLConverterTest, matchDiagnosticMessagesSequentially) {
63- llvm::SmallString<128 > EnglishLocalization (getDefaultLocalizationPath ());
64- llvm::sys::path::append (EnglishLocalization, " en" );
65- llvm::sys::path::replace_extension (EnglishLocalization, " .yaml" );
66- YAMLLocalizationProducer yaml (EnglishLocalization.str ());
79+ llvm::SmallString<128 > defYAMLConverter (getDefToYAMLConverterPath ());
80+ defYAMLConverter.append (" --output-filename=" );
81+
82+ llvm::SmallString<128 > tempFilename;
83+ std::error_code EC =
84+ llvm::sys::fs::createTemporaryFile (" en" , " yaml" , tempFilename);
85+ ASSERT_FALSE (EC);
86+ llvm::sys::RemoveFileOnSignal (tempFilename);
87+ defYAMLConverter.append (tempFilename);
88+ std::system (defYAMLConverter.c_str ());
6789
90+ YAMLLocalizationProducer yaml (tempFilename.str ());
6891 yaml.forEachAvailable ([](swift::DiagID id, llvm::StringRef translation) {
6992 llvm::StringRef msg = diagnosticMessages[static_cast <uint32_t >(id)];
7093 ASSERT_EQ (msg, translation);
7194 });
7295}
7396
7497TEST (DefToYAMLConverterTest, matchDiagnosticMessagesRandomly) {
75- llvm::SmallString<128 > EnglishLocalization (getDefaultLocalizationPath ());
76- llvm::sys::path::append (EnglishLocalization, " en" );
77- llvm::sys::path::replace_extension (EnglishLocalization, " .yaml" );
78- YAMLLocalizationProducer yaml (EnglishLocalization.str ());
98+ llvm::SmallString<128 > defYAMLConverter (getDefToYAMLConverterPath ());
99+ defYAMLConverter.append (" --output-filename=" );
100+
101+ llvm::SmallString<128 > tempFilename;
102+ std::error_code EC =
103+ llvm::sys::fs::createTemporaryFile (" en" , " yaml" , tempFilename);
104+ ASSERT_FALSE (EC);
105+ llvm::sys::RemoveFileOnSignal (tempFilename);
106+ defYAMLConverter.append (tempFilename);
107+ std::system (defYAMLConverter.c_str ());
108+
109+ YAMLLocalizationProducer yaml (tempFilename.str ());
79110
80111 std::random_device rd;
81112 std::mt19937 gen (rd ());
0 commit comments