Skip to content

Commit cdb96a0

Browse files
committed
Clean up test
1 parent c962d06 commit cdb96a0

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
#include <cmdstan/stansummary_helper.hpp>
2-
#include <stan/io/stan_csv_reader.hpp>
3-
#include <stan/services/error_codes.hpp>
41
#include <test/utility.hpp>
52
#include <gtest/gtest.h>
63

74
using cmdstan::test::convert_model_path;
5+
using cmdstan::test::run_command;
6+
using cmdstan::test::temporary_unwritable_file;
87

9-
TEST(interface, unwritable_file) {
10-
std::string model = convert_model_path(
11-
std::vector{"src", "test", "test-models", "test_model"});
12-
std::string output
13-
= convert_model_path(std::vector{"test", "output_unwritable.csv"});
8+
TEST(interface, fails_when_output_is_unwritable) {
9+
// makes a file without the w bit set
10+
temporary_unwritable_file file(
11+
convert_model_path(std::vector{"test", "output_unwritable.csv"}));
1412

15-
cmdstan::test::temporary_unwritable_file guard(output);
13+
std::string command = convert_model_path(std::vector{
14+
"src", "test", "test-models", "test_model"})
15+
+ " sample output file=" + file.filename;
1616

17-
std::string command
18-
= model + " sample num_warmup=1 num_samples=1 output file=" + output;
19-
20-
cmdstan::test::run_command_output out = cmdstan::test::run_command(command);
17+
auto out = run_command(command);
2118
EXPECT_IN_STRING("Permission denied", out.output);
2219
EXPECT_TRUE(out.hasError);
2320
}

src/test/utility.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,11 @@ void make_writable(const std::string &filename) {
343343
} // namespace internal
344344

345345
struct temporary_unwritable_file {
346-
std::string filename;
347-
temporary_unwritable_file(const std::string &filename) : filename(filename) {
346+
public:
347+
const std::string filename;
348+
349+
explicit temporary_unwritable_file(std::string filename)
350+
: filename(filename) {
348351
{
349352
// this will create the file if it does not exist
350353
std::ofstream ofs(filename);
@@ -353,6 +356,7 @@ struct temporary_unwritable_file {
353356
EXPECT_TRUE(file_exists(filename));
354357
internal::make_unwritable(filename);
355358
}
359+
356360
~temporary_unwritable_file() noexcept(false) {
357361
internal::make_writable(filename);
358362
EXPECT_EQ(remove(filename.c_str()), 0);

0 commit comments

Comments
 (0)