Ejemplo n.º 1
0
    def test_get_filepaths_by_extensions(self):
        """Test get_filepaths_by_extensions only returns filepaths in
        directory with given extensions.
        """
        filepaths = []
        build.ensure_directory_exists(MOCK_ASSETS_DEV_DIR)
        extensions = ('.json', '.svg',)

        self.assertEqual(len(filepaths), 0)
        filepaths = build.get_filepaths_by_extensions(
            MOCK_ASSETS_DEV_DIR, extensions)
        for filepath in filepaths:
            self.assertTrue(any(filepath.endswith(p) for p in extensions))
        file_count = 0
        for _, _, filenames in os.walk(MOCK_ASSETS_DEV_DIR):
            for filename in filenames:
                if any(filename.endswith(p) for p in extensions):
                    file_count += 1
        self.assertEqual(len(filepaths), file_count)

        filepaths = []
        extensions = ('.pdf', '.viminfo', '.idea',)

        self.assertEqual(len(filepaths), 0)
        filepaths = build.get_filepaths_by_extensions(
            MOCK_ASSETS_DEV_DIR, extensions)
        self.assertEqual(len(filepaths), 0)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    def test_generate_build_tasks_to_build_directory(self):
        """Test generate_build_tasks_to_build_directory queues up a
        corresponding number of build tasks according to the given scenario.
        """
        EXTENSIONS_DIRNAMES_TO_DIRPATHS = {
            'dev_dir': MOCK_EXTENSIONS_DEV_DIR,
            'compiled_js_dir': MOCK_EXTENSIONS_COMPILED_JS_DIR,
            'staging_dir': os.path.join(
                TEST_DIR, 'backend_prod_files', 'extensions', ''),
            'out_dir': os.path.join(TEST_DIR, 'build', 'extensions', '')
        }
        file_hashes = build.get_file_hashes(MOCK_EXTENSIONS_DEV_DIR)
        compiled_js_file_hashes = build.get_file_hashes(
            MOCK_EXTENSIONS_COMPILED_JS_DIR)
        build_dir_tasks = collections.deque()
        build_all_files_tasks = (
            build.generate_build_tasks_to_build_all_files_in_directory(
                MOCK_EXTENSIONS_DEV_DIR,
                EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir'],
                file_hashes))
        build_all_files_tasks += (
            build.generate_build_tasks_to_build_all_files_in_directory(
                MOCK_EXTENSIONS_COMPILED_JS_DIR,
                EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir'],
                compiled_js_file_hashes))
        self.assertGreater(len(build_all_files_tasks), 0)

        # Test for building all files when staging dir does not exist.
        self.assertEqual(len(build_dir_tasks), 0)
        build_dir_tasks += build.generate_build_tasks_to_build_directory(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS, file_hashes)
        self.assertEqual(len(build_dir_tasks), len(build_all_files_tasks))

        build.safe_delete_directory_tree(TEST_DIR)
        build_dir_tasks.clear()

        # Test for building only new files when staging dir exists.
        build.ensure_directory_exists(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS['staging_dir'])
        self.assertEqual(len(build_dir_tasks), 0)

        source_hashes = file_hashes
        source_hashes.update(compiled_js_file_hashes)
        build_dir_tasks += build.generate_build_tasks_to_build_directory(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS, source_hashes)
        self.assertEqual(len(build_dir_tasks), len(build_all_files_tasks))

        build.safe_delete_directory_tree(TEST_DIR)

        # Build all files and save to final directory.
        build.ensure_directory_exists(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS['staging_dir'])
        build._execute_tasks(build_dir_tasks)
        self.assertEqual(threading.active_count(), 1)
        build._execute_tasks(
            build.generate_copy_tasks_to_copy_from_source_to_target(
                EXTENSIONS_DIRNAMES_TO_DIRPATHS['staging_dir'],
                EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir'], file_hashes))

        build_dir_tasks.clear()

        # Test for only building files that need to be rebuilt.
        self.assertEqual(len(build_dir_tasks), 0)
        build_dir_tasks += build.generate_build_tasks_to_build_directory(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS, build_dir_tasks)
        file_extensions_to_always_rebuild = ('.html', '.py',)
        always_rebuilt_filepaths = build.get_filepaths_by_extensions(
            MOCK_EXTENSIONS_DEV_DIR, file_extensions_to_always_rebuild)
        self.assertGreater(len(always_rebuilt_filepaths), 0)
        self.assertEqual(len(build_dir_tasks), len(always_rebuilt_filepaths))

        build.safe_delete_directory_tree(TEST_DIR)
Ejemplo n.º 6
0
    def test_re_build_recently_changed_files_at_dev_dir(self):
        temp_file = tempfile.NamedTemporaryFile()
        temp_file.name = '%ssome_file.js' % MOCK_EXTENSIONS_DEV_DIR
        with open('%ssome_file.js' % MOCK_EXTENSIONS_DEV_DIR, 'w') as tmp:
            tmp.write('Some content.')

        EXTENSIONS_DIRNAMES_TO_DIRPATHS = {
            'dev_dir': MOCK_EXTENSIONS_DEV_DIR,
            'compiled_js_dir': MOCK_EXTENSIONS_COMPILED_JS_DIR,
            'staging_dir': os.path.join(
                TEST_DIR, 'backend_prod_files', 'extensions', ''),
            'out_dir': os.path.join(TEST_DIR, 'build', 'extensions', '')
        }

        file_hashes = build.get_file_hashes(MOCK_EXTENSIONS_DEV_DIR)
        compiled_js_file_hashes = build.get_file_hashes(
            MOCK_EXTENSIONS_COMPILED_JS_DIR)
        build_dir_tasks = collections.deque()
        build_all_files_tasks = (
            build.generate_build_tasks_to_build_all_files_in_directory(
                MOCK_EXTENSIONS_DEV_DIR,
                EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir'],
                file_hashes))
        build_all_files_tasks += (
            build.generate_build_tasks_to_build_all_files_in_directory(
                MOCK_EXTENSIONS_COMPILED_JS_DIR,
                EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir'],
                compiled_js_file_hashes))
        self.assertGreater(len(build_all_files_tasks), 0)

        # Test for building all files when staging dir does not exist.
        self.assertEqual(len(build_dir_tasks), 0)
        build_dir_tasks += build.generate_build_tasks_to_build_directory(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS, file_hashes)
        self.assertEqual(len(build_dir_tasks), len(build_all_files_tasks))

        build.safe_delete_directory_tree(TEST_DIR)
        build_dir_tasks.clear()

        # Test for building only new files when staging dir exists.
        build.ensure_directory_exists(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS['staging_dir'])
        self.assertEqual(len(build_dir_tasks), 0)

        build_dir_tasks = build.generate_build_tasks_to_build_directory(
            EXTENSIONS_DIRNAMES_TO_DIRPATHS, {})
        file_extensions_to_always_rebuild = ('.py', '.js', '.html')
        always_rebuilt_filepaths = build.get_filepaths_by_extensions(
            MOCK_EXTENSIONS_DEV_DIR, file_extensions_to_always_rebuild)
        self.assertEqual(
            sorted(always_rebuilt_filepaths), sorted(
                ['base.py', 'CodeRepl.py', '__init__.py', 'some_file.js',
                 'DragAndDropSortInput.py', 'code_repl_prediction.html']))
        self.assertGreater(len(always_rebuilt_filepaths), 0)

        # Test that 'some_file.js' is not rebuilt, i.e it is built for the first
        # time.
        self.assertEqual(
            len(build_dir_tasks), len(always_rebuilt_filepaths) + 1)
        self.assertIn('some_file.js', always_rebuilt_filepaths)
        self.assertNotIn('some_file.js', build_dir_tasks)

        build.safe_delete_directory_tree(TEST_DIR)
        temp_file.close()