| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
| 6 #include "base/files/file_util.h" | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/platform_thread.h" | |
| 11 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "tools/gn/filesystem_utils.h" | 10 #include "tools/gn/filesystem_utils.h" |
| 14 #include "tools/gn/target.h" | 11 #include "tools/gn/target.h" |
| 15 | 12 |
| 16 TEST(FilesystemUtils, FileExtensionOffset) { | 13 TEST(FilesystemUtils, FileExtensionOffset) { |
| 17 EXPECT_EQ(std::string::npos, FindExtensionOffset("")); | 14 EXPECT_EQ(std::string::npos, FindExtensionOffset("")); |
| 18 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz")); | 15 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz")); |
| 19 EXPECT_EQ(4u, FindExtensionOffset("foo.")); | 16 EXPECT_EQ(4u, FindExtensionOffset("foo.")); |
| 20 EXPECT_EQ(4u, FindExtensionOffset("f.o.bar")); | 17 EXPECT_EQ(4u, FindExtensionOffset("f.o.bar")); |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 EXPECT_EQ("/SOURCE/foo/bar/", SourceDirForPath(root, | 547 EXPECT_EQ("/SOURCE/foo/bar/", SourceDirForPath(root, |
| 551 base::FilePath("/SOURCE/foo/bar/")).value()); | 548 base::FilePath("/SOURCE/foo/bar/")).value()); |
| 552 | 549 |
| 553 // Empty source dir. | 550 // Empty source dir. |
| 554 base::FilePath empty; | 551 base::FilePath empty; |
| 555 EXPECT_EQ("/source/foo/", | 552 EXPECT_EQ("/source/foo/", |
| 556 SourceDirForPath(empty, base::FilePath("/source/foo")).value()); | 553 SourceDirForPath(empty, base::FilePath("/source/foo")).value()); |
| 557 #endif | 554 #endif |
| 558 } | 555 } |
| 559 | 556 |
| 560 TEST(FilesystemUtils, ContentsEqual) { | |
| 561 base::ScopedTempDir temp_dir; | |
| 562 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 563 | |
| 564 std::string data = "foo"; | |
| 565 | |
| 566 base::FilePath file_path = temp_dir.path().AppendASCII("foo.txt"); | |
| 567 base::WriteFile(file_path, data.c_str(), static_cast<int>(data.size())); | |
| 568 | |
| 569 EXPECT_TRUE(ContentsEqual(file_path, data)); | |
| 570 | |
| 571 // Different length and contents. | |
| 572 data += "bar"; | |
| 573 EXPECT_FALSE(ContentsEqual(file_path, data)); | |
| 574 | |
| 575 // The same length, different contents. | |
| 576 EXPECT_FALSE(ContentsEqual(file_path, "bar")); | |
| 577 } | |
| 578 | |
| 579 TEST(FilesystemUtils, WriteFileIfChanged) { | |
| 580 base::ScopedTempDir temp_dir; | |
| 581 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 582 | |
| 583 std::string data = "foo"; | |
| 584 | |
| 585 // Write if file doesn't exist. Create also directory. | |
| 586 base::FilePath file_path = | |
| 587 temp_dir.path().AppendASCII("bar").AppendASCII("foo.txt"); | |
| 588 EXPECT_TRUE(WriteFileIfChanged(file_path, data, nullptr)); | |
| 589 | |
| 590 base::File::Info file_info; | |
| 591 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info)); | |
| 592 base::Time last_modified = file_info.last_modified; | |
| 593 | |
| 594 #if defined(OS_MACOSX) | |
| 595 // Modification times are in seconds in HFS on Mac. | |
| 596 base::TimeDelta sleep_time = base::TimeDelta::FromSeconds(1); | |
| 597 #else | |
| 598 base::TimeDelta sleep_time = base::TimeDelta::FromMilliseconds(1); | |
| 599 #endif | |
| 600 base::PlatformThread::Sleep(sleep_time); | |
| 601 | |
| 602 // Don't write if contents is the same. | |
| 603 EXPECT_TRUE(WriteFileIfChanged(file_path, data, nullptr)); | |
| 604 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info)); | |
| 605 EXPECT_EQ(last_modified, file_info.last_modified); | |
| 606 | |
| 607 // Write if contents changed. | |
| 608 EXPECT_TRUE(WriteFileIfChanged(file_path, "bar", nullptr)); | |
| 609 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info)); | |
| 610 EXPECT_NE(last_modified, file_info.last_modified); | |
| 611 } | |
| 612 | |
| 613 TEST(FilesystemUtils, GetToolchainDirs) { | 557 TEST(FilesystemUtils, GetToolchainDirs) { |
| 614 BuildSettings build_settings; | 558 BuildSettings build_settings; |
| 615 build_settings.SetBuildDir(SourceDir("//out/Debug/")); | 559 build_settings.SetBuildDir(SourceDir("//out/Debug/")); |
| 616 | 560 |
| 617 // The default toolchain. | 561 // The default toolchain. |
| 618 Settings default_settings(&build_settings, ""); | 562 Settings default_settings(&build_settings, ""); |
| 619 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); | 563 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); |
| 620 default_settings.set_toolchain_label(default_toolchain_label); | 564 default_settings.set_toolchain_label(default_toolchain_label); |
| 621 default_settings.set_default_toolchain_label(default_toolchain_label); | 565 default_settings.set_default_toolchain_label(default_toolchain_label); |
| 622 | 566 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); | 721 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); |
| 778 EXPECT_EQ("//obj/", | 722 EXPECT_EQ("//obj/", |
| 779 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); | 723 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); |
| 780 EXPECT_EQ("obj/", | 724 EXPECT_EQ("obj/", |
| 781 GetOutputDirForSourceDirAsOutputFile( | 725 GetOutputDirForSourceDirAsOutputFile( |
| 782 &settings, SourceDir("//")).value()); | 726 &settings, SourceDir("//")).value()); |
| 783 EXPECT_EQ("gen/", | 727 EXPECT_EQ("gen/", |
| 784 GetGenDirForSourceDirAsOutputFile( | 728 GetGenDirForSourceDirAsOutputFile( |
| 785 &settings, SourceDir("//")).value()); | 729 &settings, SourceDir("//")).value()); |
| 786 } | 730 } |
| OLD | NEW |