|
12 | 12 | #include <string>
|
13 | 13 | #include <vector>
|
14 | 14 | #include <sys/stat.h>
|
| 15 | +#include <sys/types.h> |
15 | 16 |
|
16 | 17 | #define EXPECT_IN_STRING(needle, haystack) \
|
17 | 18 | EXPECT_TRUE(boost::algorithm::contains(haystack, needle)) \
|
@@ -318,6 +319,47 @@ bool is_valid_JSON(std::string &text) {
|
318 | 319 | return !document.Parse<0>(text.c_str()).HasParseError();
|
319 | 320 | }
|
320 | 321 |
|
| 322 | +namespace internal { |
| 323 | + |
| 324 | +// TODO: can be replaced with std::filesystem when we have better compiler |
| 325 | +// support for C++17 (currently missing from our minimum clang version) |
| 326 | +#ifdef _WIN32 |
| 327 | +#include <io.h> |
| 328 | +void make_unwritable_file(const std::string &filename) { |
| 329 | + _chmod(filename.c_str(), _S_IREAD); |
| 330 | +} |
| 331 | +void make_writable(const std::string &filename) { |
| 332 | + _chmod(filename.c_str(), _S_IWRITE); |
| 333 | +} |
| 334 | +#else |
| 335 | +void make_unwritable(const std::string &filename) { |
| 336 | + chmod(filename.c_str(), 0444); |
| 337 | +} |
| 338 | +void make_writable(const std::string &filename) { |
| 339 | + chmod(filename.c_str(), 0644); |
| 340 | +} |
| 341 | +#endif |
| 342 | + |
| 343 | +} // namespace internal |
| 344 | + |
| 345 | +struct temporary_unwritable_file { |
| 346 | + std::string filename; |
| 347 | + temporary_unwritable_file(const std::string &filename) : filename(filename) { |
| 348 | + { |
| 349 | + // this will create the file if it does not exist |
| 350 | + std::ofstream ofs(filename); |
| 351 | + ofs.close(); |
| 352 | + } |
| 353 | + EXPECT_TRUE(file_exists(filename)); |
| 354 | + internal::make_unwritable(filename); |
| 355 | + } |
| 356 | + ~temporary_unwritable_file() noexcept(false) { |
| 357 | + internal::make_writable(filename); |
| 358 | + EXPECT_EQ(remove(filename.c_str()), 0); |
| 359 | + EXPECT_FALSE(file_exists(filename)); |
| 360 | + } |
| 361 | +}; |
| 362 | + |
321 | 363 | } // namespace test
|
322 | 364 | } // namespace cmdstan
|
323 | 365 | #endif
|
0 commit comments