Commit 191ab5f2 authored by Greg Thompson's avatar Greg Thompson Committed by Commit Bot

Fix temp dir cleanup in components_unittests.

The ScopedTempDir must outlive anything that holds file(s) open within
it; otherwise, it is unable to delete itself.

BUG=546640

Change-Id: I09d5fe93f6ed0620087b9360a6922ff919221e0b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1891191Reviewed-by: default avatarJustin Donnelly <jdonnelly@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarJared Saul <jsaul@google.com>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Greg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711236}
parent 6d00e32f
...@@ -60,7 +60,7 @@ class StrikeDatabaseTest : public ::testing::Test { ...@@ -60,7 +60,7 @@ class StrikeDatabaseTest : public ::testing::Test {
StrikeDatabaseTest() {} StrikeDatabaseTest() {}
void SetUp() override { void SetUp() override {
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
db_provider_ = std::make_unique<leveldb_proto::ProtoDatabaseProvider>( db_provider_ = std::make_unique<leveldb_proto::ProtoDatabaseProvider>(
temp_dir_.GetPath()); temp_dir_.GetPath());
...@@ -129,10 +129,10 @@ class StrikeDatabaseTest : public ::testing::Test { ...@@ -129,10 +129,10 @@ class StrikeDatabaseTest : public ::testing::Test {
protected: protected:
base::HistogramTester* GetHistogramTester() { return &histogram_tester_; } base::HistogramTester* GetHistogramTester() { return &histogram_tester_; }
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
std::unique_ptr<leveldb_proto::ProtoDatabaseProvider> db_provider_; std::unique_ptr<leveldb_proto::ProtoDatabaseProvider> db_provider_;
std::unique_ptr<TestStrikeDatabase> strike_database_; std::unique_ptr<TestStrikeDatabase> strike_database_;
base::ScopedTempDir temp_dir_;
private: private:
base::HistogramTester histogram_tester_; base::HistogramTester histogram_tester_;
......
...@@ -164,11 +164,6 @@ class ProtoDatabaseImplTest : public testing::Test { ...@@ -164,11 +164,6 @@ class ProtoDatabaseImplTest : public testing::Test {
kDefaultClientName, shared_db_temp_dir_->GetPath())); kDefaultClientName, shared_db_temp_dir_->GetPath()));
} }
void TearDown() override {
temp_dir_.reset();
shared_db_temp_dir_.reset();
}
void SetUpExperimentParams(std::map<std::string, std::string> params) { void SetUpExperimentParams(std::map<std::string, std::string> params) {
scoped_feature_list_.InitAndEnableFeatureWithParameters( scoped_feature_list_.InitAndEnableFeatureWithParameters(
kProtoDBSharedMigration, params); kProtoDBSharedMigration, params);
...@@ -394,6 +389,7 @@ class ProtoDatabaseImplTest : public testing::Test { ...@@ -394,6 +389,7 @@ class ProtoDatabaseImplTest : public testing::Test {
private: private:
std::unique_ptr<base::ScopedTempDir> temp_dir_; std::unique_ptr<base::ScopedTempDir> temp_dir_;
std::unique_ptr<base::ScopedTempDir> shared_db_temp_dir_;
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
...@@ -401,7 +397,6 @@ class ProtoDatabaseImplTest : public testing::Test { ...@@ -401,7 +397,6 @@ class ProtoDatabaseImplTest : public testing::Test {
std::unique_ptr<base::Thread> test_thread_; std::unique_ptr<base::Thread> test_thread_;
std::unique_ptr<base::Thread> shared_db_thread_; std::unique_ptr<base::Thread> shared_db_thread_;
scoped_refptr<SharedProtoDatabase> shared_db_; scoped_refptr<SharedProtoDatabase> shared_db_;
std::unique_ptr<base::ScopedTempDir> shared_db_temp_dir_;
}; };
using ProtoDatabaseImplTestConfig = testing::Types<TestProto, ClientStruct>; using ProtoDatabaseImplTestConfig = testing::Types<TestProto, ClientStruct>;
......
...@@ -23,20 +23,14 @@ namespace leveldb_proto { ...@@ -23,20 +23,14 @@ namespace leveldb_proto {
class SharedProtoDatabaseClientTest : public testing::Test { class SharedProtoDatabaseClientTest : public testing::Test {
public: public:
void SetUp() override { void SetUp() override {
temp_dir_.reset(new base::ScopedTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
db_ = base::WrapRefCounted( db_ = base::WrapRefCounted(
new SharedProtoDatabase("client", temp_dir_->GetPath())); new SharedProtoDatabase("client", temp_dir_.GetPath()));
}
void TearDown() override {
db_->database_task_runner_for_testing()->DeleteSoon(FROM_HERE,
std::move(temp_dir_));
} }
protected: protected:
scoped_refptr<SharedProtoDatabase> db() { return db_; } scoped_refptr<SharedProtoDatabase> db() { return db_; }
base::ScopedTempDir* temp_dir() { return temp_dir_.get(); } base::ScopedTempDir* temp_dir() { return &temp_dir_; }
LevelDB* GetLevelDB() const { return db_->GetLevelDBForTesting(); } LevelDB* GetLevelDB() const { return db_->GetLevelDBForTesting(); }
...@@ -288,10 +282,9 @@ class SharedProtoDatabaseClientTest : public testing::Test { ...@@ -288,10 +282,9 @@ class SharedProtoDatabaseClientTest : public testing::Test {
} }
private: private:
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
scoped_refptr<SharedProtoDatabase> db_; scoped_refptr<SharedProtoDatabase> db_;
std::unique_ptr<base::ScopedTempDir> temp_dir_;
}; };
TEST_F(SharedProtoDatabaseClientTest, InitSuccess) { TEST_F(SharedProtoDatabaseClientTest, InitSuccess) {
......
...@@ -169,8 +169,9 @@ class InMemoryURLIndexTest : public testing::Test { ...@@ -169,8 +169,9 @@ class InMemoryURLIndexTest : public testing::Test {
void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, void ExpectPrivateDataEqual(const URLIndexPrivateData& expected,
const URLIndexPrivateData& actual); const URLIndexPrivateData& actual);
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir history_dir_; base::ScopedTempDir history_dir_;
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_;
std::unique_ptr<history::HistoryService> history_service_; std::unique_ptr<history::HistoryService> history_service_;
history::HistoryDatabase* history_database_ = nullptr; history::HistoryDatabase* history_database_ = nullptr;
std::unique_ptr<TemplateURLService> template_url_service_; std::unique_ptr<TemplateURLService> template_url_service_;
...@@ -229,9 +230,9 @@ bool InMemoryURLIndexTest::DeleteURL(const GURL& url) { ...@@ -229,9 +230,9 @@ bool InMemoryURLIndexTest::DeleteURL(const GURL& url) {
void InMemoryURLIndexTest::SetUp() { void InMemoryURLIndexTest::SetUp() {
// We cannot access the database until the backend has been loaded. // We cannot access the database until the backend has been loaded.
if (history_dir_.CreateUniqueTempDir()) ASSERT_TRUE(history_dir_.CreateUniqueTempDir());
history_service_ = history_service_ =
history::CreateHistoryService(history_dir_.GetPath(), true); history::CreateHistoryService(history_dir_.GetPath(), true);
ASSERT_TRUE(history_service_); ASSERT_TRUE(history_service_);
BlockUntilInMemoryURLIndexIsRefreshed(url_index_.get()); BlockUntilInMemoryURLIndexIsRefreshed(url_index_.get());
...@@ -1228,9 +1229,8 @@ TEST_F(InMemoryURLIndexTest, DISABLED_CacheSaveRestore) { ...@@ -1228,9 +1229,8 @@ TEST_F(InMemoryURLIndexTest, DISABLED_CacheSaveRestore) {
} }
TEST_F(InMemoryURLIndexTest, RebuildFromHistoryIfCacheOld) { TEST_F(InMemoryURLIndexTest, RebuildFromHistoryIfCacheOld) {
base::ScopedTempDir temp_directory; ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); set_history_dir(temp_dir_.GetPath());
set_history_dir(temp_directory.GetPath());
URLIndexPrivateData& private_data(*GetPrivateData()); URLIndexPrivateData& private_data(*GetPrivateData());
...@@ -1302,6 +1302,8 @@ TEST_F(InMemoryURLIndexTest, RebuildFromHistoryIfCacheOld) { ...@@ -1302,6 +1302,8 @@ TEST_F(InMemoryURLIndexTest, RebuildFromHistoryIfCacheOld) {
// Compare the captured and restored for equality. // Compare the captured and restored for equality.
ExpectPrivateDataEqual(*old_data, new_data); ExpectPrivateDataEqual(*old_data, new_data);
set_history_dir(base::FilePath());
} }
TEST_F(InMemoryURLIndexTest, CalculateWordStartsOffsets) { TEST_F(InMemoryURLIndexTest, CalculateWordStartsOffsets) {
...@@ -1433,8 +1435,8 @@ class InMemoryURLIndexCacheTest : public testing::Test { ...@@ -1433,8 +1435,8 @@ class InMemoryURLIndexCacheTest : public testing::Test {
void set_history_dir(const base::FilePath& dir_path); void set_history_dir(const base::FilePath& dir_path);
bool GetCacheFilePath(base::FilePath* file_path) const; bool GetCacheFilePath(base::FilePath* file_path) const;
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir temp_dir_; base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_;
std::unique_ptr<InMemoryURLIndex> url_index_; std::unique_ptr<InMemoryURLIndex> url_index_;
}; };
......
...@@ -183,11 +183,11 @@ class JsonPrefStoreTest ...@@ -183,11 +183,11 @@ class JsonPrefStoreTest
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
} }
base::test::TaskEnvironment task_environment_;
// The path to temporary directory used to contain the test operations. // The path to temporary directory used to contain the test operations.
base::ScopedTempDir temp_dir_; base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_;
DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreTest); DISALLOW_COPY_AND_ASSIGN(JsonPrefStoreTest);
}; };
......
...@@ -85,6 +85,8 @@ class SubresourceFilterRulesetPublisherImplTest : public ::testing::Test { ...@@ -85,6 +85,8 @@ class SubresourceFilterRulesetPublisherImplTest : public ::testing::Test {
content::TestBrowserContext* browser_context() { return &browser_context_; } content::TestBrowserContext* browser_context() { return &browser_context_; }
base::FilePath temp_dir() const { return scoped_temp_dir_.GetPath(); }
base::FilePath scoped_temp_file() const { base::FilePath scoped_temp_file() const {
return scoped_temp_dir_.GetPath().AppendASCII("data"); return scoped_temp_dir_.GetPath().AppendASCII("data");
} }
...@@ -155,11 +157,8 @@ TEST_F(SubresourceFilterRulesetPublisherImplTest, ...@@ -155,11 +157,8 @@ TEST_F(SubresourceFilterRulesetPublisherImplTest,
// Regression test for crbug.com/817308. Test verifies that ruleset is // Regression test for crbug.com/817308. Test verifies that ruleset is
// published on browser startup via exactly one PostTask. // published on browser startup via exactly one PostTask.
// Create a temporary directory for the indexed ruleset data.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
const base::FilePath base_dir = const base::FilePath base_dir =
scoped_temp_dir.GetPath().AppendASCII("Rules").AppendASCII("Indexed"); temp_dir().AppendASCII("Rules").AppendASCII("Indexed");
// Create a testing ruleset. // Create a testing ruleset.
testing::TestRulesetPair ruleset; testing::TestRulesetPair ruleset;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment