def test_compare_file_count(self): """Test _compare_file_count raises exception when there is a mismatched file count between 2 dirs list. """ # Test when both lists contain single directory. build.ensure_directory_exists(EMPTY_DIR) source_dir_file_count = build.get_file_count(EMPTY_DIR) assert source_dir_file_count == 0 target_dir_file_count = build.get_file_count(MOCK_ASSETS_DEV_DIR) # Ensure that ASSETS_DEV_DIR has at least 1 file. assert target_dir_file_count > 0 with self.assertRaisesRegexp( ValueError, ( '%s files in first dir list != %s files in second dir list') % (source_dir_file_count, target_dir_file_count)): build._compare_file_count([EMPTY_DIR], [MOCK_ASSETS_DEV_DIR]) # Test when one of the lists contain multiple directories. MOCK_EXTENSIONS_DIR_LIST = [ MOCK_EXTENSIONS_DEV_DIR, MOCK_EXTENSIONS_COMPILED_JS_DIR] target_dir_file_count = build.get_file_count( MOCK_EXTENSIONS_DEV_DIR) + build.get_file_count( MOCK_EXTENSIONS_COMPILED_JS_DIR) # Ensure that MOCK_EXTENSIONS_DIR has at least 1 file. assert target_dir_file_count > 0 with self.assertRaisesRegexp( ValueError, ( '%s files in first dir list != %s files in second dir list') % (source_dir_file_count, target_dir_file_count)): build._compare_file_count([EMPTY_DIR], MOCK_EXTENSIONS_DIR_LIST) # Reset EMPTY_DIRECTORY to clean state. build.safe_delete_directory_tree(EMPTY_DIR)
def test_compare_file_count(self): """Test _compare_file_count raises exception when there is a mismatched file count between 2 dirs. """ build.ensure_directory_exists(EMPTY_DIR) source_dir_file_count = build.get_file_count(EMPTY_DIR) assert source_dir_file_count == 0 target_dir_file_count = build.get_file_count(MOCK_ASSETS_DEV_DIR) # Ensure that ASSETS_DEV_DIR has at least 1 file. assert target_dir_file_count > 0 with self.assertRaisesRegexp( ValueError, ('%s files in first dir != %s files in second dir') % (source_dir_file_count, target_dir_file_count)): build._compare_file_count(EMPTY_DIR, MOCK_ASSETS_DEV_DIR) # Reset EMPTY_DIRECTORY to clean state. build.safe_delete_directory_tree(EMPTY_DIR)
def test_generate_copy_tasks_to_copy_from_source_to_target(self): """Test generate_copy_tasks_to_copy_from_source_to_target queues up the same number of copy tasks as the number of files in the directory. """ assets_hashes = build.get_file_hashes(MOCK_ASSETS_DEV_DIR) total_file_count = build.get_file_count(MOCK_ASSETS_DEV_DIR) copy_tasks = collections.deque() self.assertEqual(len(copy_tasks), 0) copy_tasks += build.generate_copy_tasks_to_copy_from_source_to_target( MOCK_ASSETS_DEV_DIR, MOCK_ASSETS_OUT_DIR, assets_hashes) self.assertEqual(len(copy_tasks), total_file_count)
def test_generate_delete_tasks_to_remove_deleted_files(self): """Test generate_delete_tasks_to_remove_deleted_files queues up the same number of deletion task as the number of deleted files. """ delete_tasks = collections.deque() # The empty dict means that all files should be removed. file_hashes = dict() self.assertEqual(len(delete_tasks), 0) delete_tasks += build.generate_delete_tasks_to_remove_deleted_files( file_hashes, MOCK_TEMPLATES_DEV_DIR) self.assertEqual( len(delete_tasks), build.get_file_count(MOCK_TEMPLATES_DEV_DIR))
def test_generate_build_tasks_to_build_all_files_in_directory(self): """Test generate_build_tasks_to_build_all_files_in_directory queues up the same number of build tasks as the number of files in the source directory. """ asset_hashes = build.get_file_hashes(MOCK_ASSETS_DEV_DIR) tasks = collections.deque() self.assertEqual(len(tasks), 0) # Build all files. tasks = build.generate_build_tasks_to_build_all_files_in_directory( MOCK_ASSETS_DEV_DIR, MOCK_ASSETS_OUT_DIR, asset_hashes) total_file_count = build.get_file_count(MOCK_ASSETS_DEV_DIR) self.assertEqual(len(tasks), total_file_count)
def test_get_file_count(self): """Test get_file_count returns the correct number of files, excluding file with extensions in FILE_EXTENSIONS_TO_IGNORE and files that should not be built. """ all_inclusive_file_count = 0 for _, _, files in os.walk(MOCK_EXTENSIONS_DEV_DIR): all_inclusive_file_count += len(files) ignored_file_count = 0 for _, _, files in os.walk(MOCK_EXTENSIONS_DEV_DIR): for filename in files: if not build.should_file_be_built(filename) or any( filename.endswith(p) for p in build.FILE_EXTENSIONS_TO_IGNORE): ignored_file_count += 1 self.assertEqual(all_inclusive_file_count - ignored_file_count, build.get_file_count(MOCK_EXTENSIONS_DEV_DIR))
def test_get_recently_changed_filenames(self): """Test get_recently_changed_filenames detects file recently added.""" # Create an empty folder. build.ensure_directory_exists(EMPTY_DIR) # Get hashes from ASSETS_DEV_DIR to simulate a folder with built files. assets_hashes = build.get_file_hashes(MOCK_ASSETS_DEV_DIR) recently_changed_filenames = [] self.assertEqual(len(recently_changed_filenames), 0) recently_changed_filenames = build.get_recently_changed_filenames( assets_hashes, EMPTY_DIR) # Since all HTML and Python files are already built, they are ignored. with self.swap(build, 'FILE_EXTENSIONS_TO_IGNORE', ('.html', '.py',)): self.assertEqual( len(recently_changed_filenames), build.get_file_count( MOCK_ASSETS_DEV_DIR)) build.safe_delete_directory_tree(EMPTY_DIR)