| 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)
|
|
|