1 // Copyright 2018 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 "gn/setup.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "gn/filesystem_utils.h"
11 #include "gn/switches.h"
12 #include "gn/test_with_scheduler.h"
13
14 using SetupTest = TestWithScheduler;
15
WriteFile(const base::FilePath & file,const std::string & data)16 static void WriteFile(const base::FilePath& file, const std::string& data) {
17 CHECK_EQ(static_cast<int>(data.size()), // Way smaller than INT_MAX.
18 base::WriteFile(file, data.data(), data.size()));
19 }
20
TEST_F(SetupTest,DotGNFileIsGenDep)21 TEST_F(SetupTest, DotGNFileIsGenDep) {
22 base::CommandLine cmdline(base::CommandLine::NO_PROGRAM);
23
24 // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file,
25 // pass it as --root.
26 base::ScopedTempDir in_temp_dir;
27 ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir());
28 base::FilePath in_path = in_temp_dir.GetPath();
29 base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn"));
30 WriteFile(dot_gn_name, "buildconfig = \"//BUILDCONFIG.gn\"\n");
31 WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), "");
32 cmdline.AppendSwitchASCII(switches::kRoot, FilePathToUTF8(in_path));
33
34 // Create another temp dir for writing the generated files to.
35 base::ScopedTempDir build_temp_dir;
36 ASSERT_TRUE(build_temp_dir.CreateUniqueTempDir());
37
38 // Run setup and check that the .gn file is in the scheduler's gen deps.
39 Setup setup;
40 EXPECT_TRUE(
41 setup.DoSetup(FilePathToUTF8(build_temp_dir.GetPath()), true, cmdline));
42 std::vector<base::FilePath> gen_deps = g_scheduler->GetGenDependencies();
43 ASSERT_EQ(1u, gen_deps.size());
44 EXPECT_EQ(gen_deps[0], base::MakeAbsoluteFilePath(dot_gn_name));
45 }
46