| 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" |
| 6 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/platform_thread.h" |
| 8 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "tools/gn/filesystem_utils.h" | 13 #include "tools/gn/filesystem_utils.h" |
| 11 #include "tools/gn/target.h" | 14 #include "tools/gn/target.h" |
| 12 | 15 |
| 13 TEST(FilesystemUtils, FileExtensionOffset) { | 16 TEST(FilesystemUtils, FileExtensionOffset) { |
| 14 EXPECT_EQ(std::string::npos, FindExtensionOffset("")); | 17 EXPECT_EQ(std::string::npos, FindExtensionOffset("")); |
| 15 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz")); | 18 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz")); |
| 16 EXPECT_EQ(4u, FindExtensionOffset("foo.")); | 19 EXPECT_EQ(4u, FindExtensionOffset("foo.")); |
| 17 EXPECT_EQ(4u, FindExtensionOffset("f.o.bar")); | 20 EXPECT_EQ(4u, FindExtensionOffset("f.o.bar")); |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 EXPECT_EQ("/SOURCE/foo/bar/", SourceDirForPath(root, | 550 EXPECT_EQ("/SOURCE/foo/bar/", SourceDirForPath(root, |
| 548 base::FilePath("/SOURCE/foo/bar/")).value()); | 551 base::FilePath("/SOURCE/foo/bar/")).value()); |
| 549 | 552 |
| 550 // Empty source dir. | 553 // Empty source dir. |
| 551 base::FilePath empty; | 554 base::FilePath empty; |
| 552 EXPECT_EQ("/source/foo/", | 555 EXPECT_EQ("/source/foo/", |
| 553 SourceDirForPath(empty, base::FilePath("/source/foo")).value()); | 556 SourceDirForPath(empty, base::FilePath("/source/foo")).value()); |
| 554 #endif | 557 #endif |
| 555 } | 558 } |
| 556 | 559 |
| 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 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 594 |
| 595 // Don't write if contents is the same. |
| 596 EXPECT_TRUE(WriteFileIfChanged(file_path, data, nullptr)); |
| 597 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info)); |
| 598 EXPECT_EQ(last_modified, file_info.last_modified); |
| 599 |
| 600 // Write if contents changed. |
| 601 EXPECT_TRUE(WriteFileIfChanged(file_path, "bar", nullptr)); |
| 602 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info)); |
| 603 EXPECT_NE(last_modified, file_info.last_modified); |
| 604 } |
| 605 |
| 557 TEST(FilesystemUtils, GetToolchainDirs) { | 606 TEST(FilesystemUtils, GetToolchainDirs) { |
| 558 BuildSettings build_settings; | 607 BuildSettings build_settings; |
| 559 build_settings.SetBuildDir(SourceDir("//out/Debug/")); | 608 build_settings.SetBuildDir(SourceDir("//out/Debug/")); |
| 560 | 609 |
| 561 // The default toolchain. | 610 // The default toolchain. |
| 562 Settings default_settings(&build_settings, ""); | 611 Settings default_settings(&build_settings, ""); |
| 563 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); | 612 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); |
| 564 default_settings.set_toolchain_label(default_toolchain_label); | 613 default_settings.set_toolchain_label(default_toolchain_label); |
| 565 default_settings.set_default_toolchain_label(default_toolchain_label); | 614 default_settings.set_default_toolchain_label(default_toolchain_label); |
| 566 | 615 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); | 770 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); |
| 722 EXPECT_EQ("//obj/", | 771 EXPECT_EQ("//obj/", |
| 723 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); | 772 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); |
| 724 EXPECT_EQ("obj/", | 773 EXPECT_EQ("obj/", |
| 725 GetOutputDirForSourceDirAsOutputFile( | 774 GetOutputDirForSourceDirAsOutputFile( |
| 726 &settings, SourceDir("//")).value()); | 775 &settings, SourceDir("//")).value()); |
| 727 EXPECT_EQ("gen/", | 776 EXPECT_EQ("gen/", |
| 728 GetGenDirForSourceDirAsOutputFile( | 777 GetGenDirForSourceDirAsOutputFile( |
| 729 &settings, SourceDir("//")).value()); | 778 &settings, SourceDir("//")).value()); |
| 730 } | 779 } |
| OLD | NEW |