15
15
#include " llvm/ADT/StringExtras.h"
16
16
#include " llvm/ADT/StringRef.h"
17
17
#include " llvm/Support/Path.h"
18
+ #include " llvm/Support/Signals.h"
19
+ #include " llvm/Support/ToolOutputFile.h"
18
20
#include " llvm/Support/YAMLParser.h"
19
21
#include " llvm/Support/YAMLTraits.h"
20
22
#include " llvm/Support/raw_ostream.h"
21
23
#include " gtest/gtest.h"
24
+ #include < cstdlib>
22
25
#include < random>
23
26
#include < string>
27
+ #include < system_error>
24
28
25
29
using namespace swift ;
26
30
using namespace swift ::diag;
@@ -36,16 +40,28 @@ static constexpr const char *const diagnosticMessages[] = {
36
40
#include " swift/AST/DiagnosticsAll.def"
37
41
};
38
42
39
- static std::string getDefaultLocalizationPath () {
43
+ static std::string getMainExecutablePath () {
40
44
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 ());
44
53
llvm::sys::path::append (DefaultDiagnosticMessagesDir, " share" , " swift" ,
45
54
" diagnostics" );
46
55
return std::string (DefaultDiagnosticMessagesDir.str ());
47
56
}
48
57
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
+
49
65
// / Random number in [0,n)
50
66
unsigned randNum (unsigned n) { return unsigned (rand ()) % n; }
51
67
@@ -60,22 +76,37 @@ TEST(DefToYAMLConverterTest, missingLocalizationFiles) {
60
76
}
61
77
62
78
TEST (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 ());
67
89
90
+ YAMLLocalizationProducer yaml (tempFilename.str ());
68
91
yaml.forEachAvailable ([](swift::DiagID id, llvm::StringRef translation) {
69
92
llvm::StringRef msg = diagnosticMessages[static_cast <uint32_t >(id)];
70
93
ASSERT_EQ (msg, translation);
71
94
});
72
95
}
73
96
74
97
TEST (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 ());
79
110
80
111
std::random_device rd;
81
112
std::mt19937 gen (rd ());
0 commit comments