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

Unified Diff: chrome/test/mini_installer/file_visitor.py

Issue 2747023002: Cleanup machine based on the state in configuration file for mini installer test.
Patch Set: fix gni file Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/mini_installer/file_visitor.py
diff --git a/chrome/test/mini_installer/file_visitor.py b/chrome/test/mini_installer/file_visitor.py
new file mode 100644
index 0000000000000000000000000000000000000000..0254a3bf8c673c676e8e83cf7ed5bf5cb0d2f9b3
--- /dev/null
+++ b/chrome/test/mini_installer/file_visitor.py
@@ -0,0 +1,64 @@
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+import shutil
+
+import visitor
+
+class FileVisitor(visitor.Visitor):
+ """ Visitor for file entry in property
+ """
+ def _VisitEntry(self, entry_name, entry_value):
+ """ Check whether a file is existed or not.
+
+ Args:
+ 'entry_name' - A string represents the path to the file being
+ verified.
+ 'entry_value' - A dictionary with the following key and value:
+ 'exists': a boolean indicating whether the file should
+ exist.
+ """
+ file_path = self._variable_expander.Expand(entry_name)
+ is_file_exists = os.path.exists(file_path)
+ should_file_exist = entry_value['exists']
+ self._HandleFile(file_path, is_file_exists, should_file_exist)
+
+ def _HandleFile(self, file_path, is_file_exists, should_file_exist):
+ """ Abstract function to check a file."
+
+ Args:
+ file_path: A string represents the path of the file.
+ is_file_exists: A boolean represents whether the file existed.
+ should_file_exist: A boolean represents whether the file should exist.
+ """
+ raise NotImplementedError()
+
+
+class FileVerifier(FileVisitor):
+ """Verifies that the current files match the expectation dictionaries."""
+ def _HandleFile(self, file_path, is_file_exists, should_file_exist):
+ """ Overridden FileVisitor._HandleFile.
+
+ Throw AssertionError if |is_file_exists| doesn't match
+ |should_file_exist|.
+ """
+ error_message = ""
+ if is_file_exists:
+ error_message = 'File %s exists' % file_path
+ else:
+ error_message = 'File %s is missing' % file_path
+ assert should_file_exist == is_file_exists, error_message
+
+class FileCleaner(FileVisitor):
+ """ Reset the current file system state to match the expectation.
+ """
+ def _HandleFile(self, file_path, is_file_exists, should_file_exist):
+ """ Overridden FileVisitor._HandleFile.
+
+ Delete the file if file is existed but shouldn't exist. Only support
+ Directory.
+ """
+ if is_file_exists and not should_file_exist and os.path.isdir(file_path):
+ shutil.rmtree(file_path, ignore_errors=True)

Powered by Google App Engine
This is Rietveld 408576698