Roll DEPS for PDFium to 19ae17578f99621100a26dac3e2c7c3dbf7c7cd1
[chromium-blink-merge.git] / tools / gn / function_write_file_unittest.cc
blob90bfa4c81eb8f6f46014a1a51e0536e4cb4b5351
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/test_with_scope.h"
12 namespace {
14 // Returns true on success, false if write_file signaled an error.
15 bool CallWriteFile(Scope* scope,
16 const std::string& filename,
17 const Value& data) {
18 Err err;
20 std::vector<Value> args;
21 args.push_back(Value(nullptr, filename));
22 args.push_back(data);
24 FunctionCallNode function_call;
25 Value result = functions::RunWriteFile(scope, &function_call, args, &err);
26 EXPECT_EQ(Value::NONE, result.type()); // Should always return none.
28 return !err.has_error();
31 } // namespace
33 TEST(WriteFile, WithData) {
34 TestWithScope setup;
36 // Make a real directory for writing the files.
37 base::ScopedTempDir temp_dir;
38 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
39 setup.build_settings()->SetRootPath(temp_dir.path());
40 setup.build_settings()->SetBuildDir(SourceDir("//out/"));
42 Value some_string(nullptr, "some string contents");
44 // Should refuse to write files outside of the output dir.
45 EXPECT_FALSE(CallWriteFile(setup.scope(), "//in_root.txt", some_string));
46 EXPECT_FALSE(CallWriteFile(setup.scope(), "//other_dir/foo.txt",
47 some_string));
49 // Should be able to write to a new dir inside the out dir.
50 EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_string));
51 base::FilePath foo_name = temp_dir.path().Append(FILE_PATH_LITERAL("out"))
52 .Append(FILE_PATH_LITERAL("foo.txt"));
53 std::string result_contents;
54 EXPECT_TRUE(base::ReadFileToString(foo_name, &result_contents));
55 EXPECT_EQ(some_string.string_value(), result_contents);
57 // Update the contents with a list of a string and a number.
58 Value some_list(nullptr, Value::LIST);
59 some_list.list_value().push_back(Value(nullptr, "line 1"));
60 some_list.list_value().push_back(Value(nullptr, static_cast<int64>(2)));
61 EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_list));
62 EXPECT_TRUE(base::ReadFileToString(foo_name, &result_contents));
63 EXPECT_EQ("line 1\n2\n", result_contents);
65 // Test that the file is not rewritten if the contents are not changed.
66 // Start by setting the modified time to something old to avoid clock
67 // resolution issues.
68 base::Time old_time = base::Time::Now() - base::TimeDelta::FromDays(1);
69 base::File foo_file(foo_name,
70 base::File::FLAG_OPEN |
71 base::File::FLAG_READ | base::File::FLAG_WRITE);
72 ASSERT_TRUE(foo_file.IsValid());
73 foo_file.SetTimes(old_time, old_time);
75 // Read the current time to avoid timer resolution issues when comparing
76 // below.
77 base::File::Info original_info;
78 foo_file.GetInfo(&original_info);
80 EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_list));
82 // Verify that the last modified time is the same as before.
83 base::File::Info new_info;
84 foo_file.GetInfo(&new_info);
85 EXPECT_EQ(original_info.last_modified, new_info.last_modified);