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.
4
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"
11
12namespace {
13
14// Returns true on success, false if write_file signaled an error.
15bool CallWriteFile(Scope* scope,
16                   const std::string& filename,
17                   const Value& data) {
18  Err err;
19
20  std::vector<Value> args;
21  args.push_back(Value(NULL, filename));
22  args.push_back(data);
23
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.
27
28  return !err.has_error();
29}
30
31}  // namespace
32
33TEST(WriteFile, WithData) {
34  TestWithScope setup;
35
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/"));
41
42  Value some_string(NULL, "some string contents");
43
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));
48
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);
56
57  // Update the contents with a list of a string and a number.
58  Value some_list(NULL, Value::LIST);
59  some_list.list_value().push_back(Value(NULL, "line 1"));
60  some_list.list_value().push_back(Value(NULL, 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);
64
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);
74
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);
79
80  EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_list));
81
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);
86}
87