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 |
| 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 std::string file_data; |
| 610 ASSERT_TRUE(base::ReadFileToString(file_path, &file_data)); |
| 611 EXPECT_EQ("bar", file_data); |
| 612 } |
| 613 |
557 TEST(FilesystemUtils, GetToolchainDirs) { | 614 TEST(FilesystemUtils, GetToolchainDirs) { |
558 BuildSettings build_settings; | 615 BuildSettings build_settings; |
559 build_settings.SetBuildDir(SourceDir("//out/Debug/")); | 616 build_settings.SetBuildDir(SourceDir("//out/Debug/")); |
560 | 617 |
561 // The default toolchain. | 618 // The default toolchain. |
562 Settings default_settings(&build_settings, ""); | 619 Settings default_settings(&build_settings, ""); |
563 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); | 620 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); |
564 default_settings.set_toolchain_label(default_toolchain_label); | 621 default_settings.set_toolchain_label(default_toolchain_label); |
565 default_settings.set_default_toolchain_label(default_toolchain_label); | 622 default_settings.set_default_toolchain_label(default_toolchain_label); |
566 | 623 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); | 778 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); |
722 EXPECT_EQ("//obj/", | 779 EXPECT_EQ("//obj/", |
723 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); | 780 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); |
724 EXPECT_EQ("obj/", | 781 EXPECT_EQ("obj/", |
725 GetOutputDirForSourceDirAsOutputFile( | 782 GetOutputDirForSourceDirAsOutputFile( |
726 &settings, SourceDir("//")).value()); | 783 &settings, SourceDir("//")).value()); |
727 EXPECT_EQ("gen/", | 784 EXPECT_EQ("gen/", |
728 GetGenDirForSourceDirAsOutputFile( | 785 GetGenDirForSourceDirAsOutputFile( |
729 &settings, SourceDir("//")).value()); | 786 &settings, SourceDir("//")).value()); |
730 } | 787 } |
OLD | NEW |