Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: tools/gn/filesystem_utils_unittest.cc

Issue 1656253003: [GN] Don't rewrite files with the same contents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 std::stringstream stream_1;
570 stream_1 << data;
571 EXPECT_TRUE(ContentsEqual(file_path, &stream_1));
572
573 // Different length and contents.
574 stream_1 << "bar";
575 EXPECT_FALSE(ContentsEqual(file_path, &stream_1));
576
577 // The same length, different contents.
578 std::stringstream stream_2("bar");
579 EXPECT_FALSE(ContentsEqual(file_path, &stream_2));
580 }
581
582 TEST(FilesystemUtils, WriteFileIfChanged) {
583 base::ScopedTempDir temp_dir;
584 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
585
586 std::string data = "foo";
587 std::stringstream data_stream;
588 data_stream << data;
589
590 // Write if file doesn't exist.
591 base::FilePath file_path = temp_dir.path().AppendASCII("foo.txt");
592 EXPECT_TRUE(WriteFileIfChanged(file_path, &data_stream));
593
594 base::File::Info file_info;
595 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info));
596 base::Time last_modified = file_info.last_modified;
597 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
598
599 // Don't write if contents is the same.
600 EXPECT_TRUE(WriteFileIfChanged(file_path, &data_stream));
601 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info));
602 EXPECT_EQ(last_modified, file_info.last_modified);
603
604 // Write if contents changed.
605 data_stream << "bar";
606 EXPECT_TRUE(WriteFileIfChanged(file_path, &data_stream));
607 ASSERT_TRUE(base::GetFileInfo(file_path, &file_info));
608 EXPECT_NE(last_modified, file_info.last_modified);
609 }
610
557 TEST(FilesystemUtils, GetToolchainDirs) { 611 TEST(FilesystemUtils, GetToolchainDirs) {
558 BuildSettings build_settings; 612 BuildSettings build_settings;
559 build_settings.SetBuildDir(SourceDir("//out/Debug/")); 613 build_settings.SetBuildDir(SourceDir("//out/Debug/"));
560 614
561 // The default toolchain. 615 // The default toolchain.
562 Settings default_settings(&build_settings, ""); 616 Settings default_settings(&build_settings, "");
563 Label default_toolchain_label(SourceDir("//toolchain/"), "default"); 617 Label default_toolchain_label(SourceDir("//toolchain/"), "default");
564 default_settings.set_toolchain_label(default_toolchain_label); 618 default_settings.set_toolchain_label(default_toolchain_label);
565 default_settings.set_default_toolchain_label(default_toolchain_label); 619 default_settings.set_default_toolchain_label(default_toolchain_label);
566 620
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); 775 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value());
722 EXPECT_EQ("//obj/", 776 EXPECT_EQ("//obj/",
723 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); 777 GetOutputDirForSourceDir(&settings, SourceDir("//")).value());
724 EXPECT_EQ("obj/", 778 EXPECT_EQ("obj/",
725 GetOutputDirForSourceDirAsOutputFile( 779 GetOutputDirForSourceDirAsOutputFile(
726 &settings, SourceDir("//")).value()); 780 &settings, SourceDir("//")).value());
727 EXPECT_EQ("gen/", 781 EXPECT_EQ("gen/",
728 GetGenDirForSourceDirAsOutputFile( 782 GetGenDirForSourceDirAsOutputFile(
729 &settings, SourceDir("//")).value()); 783 &settings, SourceDir("//")).value());
730 } 784 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698