1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "tools/gn/functions.h"
10 #include "tools/gn/scheduler.h"
11 #include "tools/gn/test_with_scope.h"
15 // Returns true on success, false if write_file signaled an error.
16 bool CallWriteFile(Scope
* scope
,
17 const std::string
& filename
,
21 std::vector
<Value
> args
;
22 args
.push_back(Value(nullptr, filename
));
25 FunctionCallNode function_call
;
26 Value result
= functions::RunWriteFile(scope
, &function_call
, args
, &err
);
27 EXPECT_EQ(Value::NONE
, result
.type()); // Should always return none.
29 return !err
.has_error();
34 TEST(WriteFile
, WithData
) {
38 // Make a real directory for writing the files.
39 base::ScopedTempDir temp_dir
;
40 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
41 setup
.build_settings()->SetRootPath(temp_dir
.path());
42 setup
.build_settings()->SetBuildDir(SourceDir("//out/"));
44 Value
some_string(nullptr, "some string contents");
46 // Should refuse to write files outside of the output dir.
47 EXPECT_FALSE(CallWriteFile(setup
.scope(), "//in_root.txt", some_string
));
48 EXPECT_FALSE(CallWriteFile(setup
.scope(), "//other_dir/foo.txt",
51 // Should be able to write to a new dir inside the out dir.
52 EXPECT_TRUE(CallWriteFile(setup
.scope(), "//out/foo.txt", some_string
));
53 base::FilePath foo_name
= temp_dir
.path().Append(FILE_PATH_LITERAL("out"))
54 .Append(FILE_PATH_LITERAL("foo.txt"));
55 std::string result_contents
;
56 EXPECT_TRUE(base::ReadFileToString(foo_name
, &result_contents
));
57 EXPECT_EQ(some_string
.string_value(), result_contents
);
59 // Update the contents with a list of a string and a number.
60 Value
some_list(nullptr, Value::LIST
);
61 some_list
.list_value().push_back(Value(nullptr, "line 1"));
62 some_list
.list_value().push_back(Value(nullptr, static_cast<int64
>(2)));
63 EXPECT_TRUE(CallWriteFile(setup
.scope(), "//out/foo.txt", some_list
));
64 EXPECT_TRUE(base::ReadFileToString(foo_name
, &result_contents
));
65 EXPECT_EQ("line 1\n2\n", result_contents
);
67 // Test that the file is not rewritten if the contents are not changed.
68 // Start by setting the modified time to something old to avoid clock
70 base::Time old_time
= base::Time::Now() - base::TimeDelta::FromDays(1);
71 base::File
foo_file(foo_name
,
72 base::File::FLAG_OPEN
|
73 base::File::FLAG_READ
| base::File::FLAG_WRITE
);
74 ASSERT_TRUE(foo_file
.IsValid());
75 foo_file
.SetTimes(old_time
, old_time
);
77 // Read the current time to avoid timer resolution issues when comparing
79 base::File::Info original_info
;
80 foo_file
.GetInfo(&original_info
);
82 EXPECT_TRUE(CallWriteFile(setup
.scope(), "//out/foo.txt", some_list
));
84 // Verify that the last modified time is the same as before.
85 base::File::Info new_info
;
86 foo_file
.GetInfo(&new_info
);
87 EXPECT_EQ(original_info
.last_modified
, new_info
.last_modified
);