def test_content_property(self): path_to_test_file = os.path.join(self.test_env_dir, 'test.js') content = 'var foo = {};' helpers.create_file_with_content(path_to_test_file, content) resource = Resource(path_to_test_file) self.assertEquals(content, resource.content) helpers.clean_up_files(path_to_test_file)
def test_find_all_javascript_resources_in_the_paths(self): paths_to_test_files = [os.path.join(self.test_env_dir, 'test.js'), os.path.join(self.test_env_dir, 'test.css'), os.path.join(self.test_env_dir, 'test.html')] helpers.create_files(paths_to_test_files) resources = Resource.find_all_of_type_in_paths('javascript', Paths(self.test_env_dir, include_cwd=False)) helpers.clean_up_files(paths_to_test_files) self.assertEquals(1, len(resources), 'One and only one javascript file should be found')
def tests_config_file_passed_as_an_argument_overrides_the_file_in_dot_blend(self): # Step 1: Assert the default configuration with no config files app = Application() self.assertTrue(app.config.analyzers, "expected the default analyzer list to be non-empty") self.assertTrue(app.config.minifiers, "expected the default minifier list to be non-empty") # Step 2: Assert that config is loaded from .blend/config.json clean_up_files(self.default_config_file_path) create_file_with_content(self.default_config_file_path, '{}') app = Application() self.assertIsNotNone(app.config) self.assertFalse(app.config.analyzers, "expected the analyzer list to be empty") self.assertFalse(app.config.minifiers, "expected the minifier list to be empty") # Step 3: Assert that a custom config file overrides .blend/config.json clean_up_files(self.test_config_file_path) create_file_with_content(self.test_config_file_path, """{ "analyzers": { "javascript": [ { "name": "blend.SizeAnalyzer", "skip_list": [ "bin" ] } ] } }""") app = Application(config_file_path=self.test_config_file_path) self.assertIsNotNone(app.config) self.assertEqual(1, len(app.config.analyzers), "expected one analyzer") self.assertFalse(app.config.minifiers, "expected the minifier list to be empty")
def test_merge_library_required_at_the_top_of_multiple_files(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir11', 'file1.js'), os.path.join(self.test_env_dir, 'dir21', 'file2.js'), os.path.join(self.test_env_dir, 'dir31', 'file3.js'), os.path.join(self.test_env_dir, 'dir41', 'file4.js') ] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content( paths_to_test_files[0], '//= require file2\n//= require file3\n// This is file 1\n') helpers.create_file_with_content( paths_to_test_files[1], '//= require file4\n// This is file 2\n') helpers.create_file_with_content( paths_to_test_files[2], '//= require file4\n// This is file 3\n') helpers.create_file_with_content(paths_to_test_files[3], '// This is file 4\n') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths( Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual( '// This is file 4\n// This is file 2\n// This is file 3\n// This is file 1\n', actual_merged_content) helpers.clean_up_files(paths_to_test_files)
def test_run_with_single_resource(self): paths_to_processable_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.js')] create_files(paths_to_processable_test_files) app = Application(file_list=[paths_to_processable_test_files[0]]) self.assertEqual(0, app.run()) clean_up_files(paths_to_processable_test_files)
def test_run_with_single_resource(self): paths_to_processable_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.js') ] create_files(paths_to_processable_test_files) app = Application(file_list=[paths_to_processable_test_files[0]]) self.assertEqual(0, app.run()) clean_up_files(paths_to_processable_test_files)
def test_requirements_property_for_resource_with_plain_content_is_none(self): path_to_test_file = os.path.join(self.test_env_dir, 'test.js') helpers.clean_up_files(path_to_test_file) content = 'var foo = {};' helpers.create_file_with_content(path_to_test_file, content) resource = Resource(path_to_test_file) self.assertTrue(resource.content == content) self.assertEqual(None, resource.requirements) helpers.clean_up_files(path_to_test_file)
def test_exists_property(self): path_to_test_file = os.path.join(self.test_env_dir, 'test.js') helpers.clean_up_files(path_to_test_file) resource = Resource(path_to_test_file) # file does not exist yet self.assertFalse(resource.exists) helpers.create_files(path_to_test_file) self.assertTrue(resource.exists) helpers.clean_up_files(path_to_test_file)
def test_css_require_comments_found_as_requirements(self): path_to_test_file = os.path.join(self.test_env_dir, 'test.css') helpers.clean_up_files(path_to_test_file) content = 'h1 {background:red;}\n /*= require something */' helpers.create_file_with_content(path_to_test_file, content) resource = Resource(path_to_test_file) self.assertTrue(resource.content == content) self.assertEqual(1, len(resource.requirements)) self.assertEqual('something', resource.requirements[0].name) self.assertEqual((21, 47), resource.requirements[0].insert_location)
def test_requirements_property_for_resource_with_plain_content_is_none( self): path_to_test_file = os.path.join(self.test_env_dir, 'test.js') helpers.clean_up_files(path_to_test_file) content = 'var foo = {};' helpers.create_file_with_content(path_to_test_file, content) resource = Resource(path_to_test_file) self.assertTrue(resource.content == content) self.assertEqual(None, resource.requirements) helpers.clean_up_files(path_to_test_file)
def test_run_with_single_resource_with_requirement(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.js'), os.path.join(self.test_env_dir, 'dir2', 'file2.js')] clean_up_files(paths_to_test_files) create_files(paths_to_test_files) create_file_with_content(paths_to_test_files[0], '// This is file 1\n//= require file2') create_file_with_content(paths_to_test_files[1], '// This is file 2') app = Application(path_list=[self.test_env_dir], file_list=[paths_to_test_files[0]]) self.assertEqual(0, app.run()) clean_up_files(paths_to_test_files)
def test_find_all_javascript_resources_in_the_paths(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'test.js'), os.path.join(self.test_env_dir, 'test.css'), os.path.join(self.test_env_dir, 'test.html') ] helpers.create_files(paths_to_test_files) resources = Resource.find_all_of_type_in_paths( 'javascript', Paths(self.test_env_dir, include_cwd=False)) helpers.clean_up_files(paths_to_test_files) self.assertEquals(1, len(resources), 'One and only one javascript file should be found')
def test_loads_config_from_file(self): # To ensure that somthing is actually being tested, I first assert that the default configuration # is non-empty app = Application() self.assertTrue(app.config.analyzers, "expected the default analyzer list to be non-empty") self.assertTrue(app.config.minifiers, "expected the default minifier list to be non-empty") clean_up_files(self.test_config_file_path) create_file_with_content(self.test_config_file_path, '{}') app = Application(config_file_path=self.test_config_file_path) self.assertIsNotNone(app.config) self.assertFalse(app.config.analyzers, "expected the analyzer list to be empty") self.assertFalse(app.config.minifiers, "expected the minifier list to be empty")
def test_javascript_requirements_are_found(self): path_to_test_file = os.path.join(self.test_env_dir, 'test.js') helpers.clean_up_files(path_to_test_file) content = '//= require jquery\nvar foo = {};//= require openlayers\n var s = some other thing\n' helpers.create_file_with_content(path_to_test_file, content) resource = Resource(path_to_test_file) self.assertTrue(resource.content == content) self.assertEqual(2, len(resource.requirements)) self.assertEqual('jquery', resource.requirements[0].name) self.assertEqual((0, 19), resource.requirements[0].insert_location) self.assertEqual('openlayers', resource.requirements[1].name) self.assertEqual((32, 55), resource.requirements[1].insert_location) helpers.clean_up_files(path_to_test_file)
def test_merge_recursive_requirements_in_global_path(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.js'), os.path.join(self.test_env_dir, 'dir2', 'file2.js'), os.path.join(self.test_env_dir, 'dir3', 'file3.js')] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content(paths_to_test_files[0], '// This is file 1\n//= require FILE2') helpers.create_file_with_content(paths_to_test_files[1], '// This is file 2\n//= require file3') helpers.create_file_with_content(paths_to_test_files[2], '// This is file 3') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths(Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual('// This is file 1\n// This is file 2\n// This is file 3', actual_merged_content) helpers.clean_up_files(paths_to_test_files)
def test_merge_js_requirements_favors_shallow_upward_matches(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'dir2', 'dir3', 'file1.js'), os.path.join(self.test_env_dir, 'dir1', 'dir2', 'file2.js'), os.path.join(self.test_env_dir, 'dir1', 'file2.js')] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content(paths_to_test_files[0], '// This is file 1\n//= require FILE2') helpers.create_file_with_content(paths_to_test_files[1], '// This is NEAR file 2') helpers.create_file_with_content(paths_to_test_files[2], '// This is FAR file 2') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths(Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual('// This is file 1\n// This is NEAR file 2', actual_merged_content)
def test_run_with_single_resource_with_requirement(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.js'), os.path.join(self.test_env_dir, 'dir2', 'file2.js') ] clean_up_files(paths_to_test_files) create_files(paths_to_test_files) create_file_with_content(paths_to_test_files[0], '// This is file 1\n//= require file2') create_file_with_content(paths_to_test_files[1], '// This is file 2') app = Application(path_list=[self.test_env_dir], file_list=[paths_to_test_files[0]]) self.assertEqual(0, app.run()) clean_up_files(paths_to_test_files)
def test_merge_library_required_at_the_top_of_multiple_files(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir11', 'file1.js'), os.path.join(self.test_env_dir, 'dir21', 'file2.js'), os.path.join(self.test_env_dir, 'dir31', 'file3.js'), os.path.join(self.test_env_dir, 'dir41', 'file4.js')] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content(paths_to_test_files[0], '//= require file2\n//= require file3\n// This is file 1\n') helpers.create_file_with_content(paths_to_test_files[1], '//= require file4\n// This is file 2\n') helpers.create_file_with_content(paths_to_test_files[2], '//= require file4\n// This is file 3\n') helpers.create_file_with_content(paths_to_test_files[3], '// This is file 4\n') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths(Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual('// This is file 4\n// This is file 2\n// This is file 3\n// This is file 1\n', actual_merged_content) helpers.clean_up_files(paths_to_test_files)
def test_merge_css_requirements_in_local_path(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.css'), os.path.join(self.test_env_dir, 'dir1', 'file2.css'), os.path.join(self.test_env_dir, 'dir2', 'file2.css')] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content(paths_to_test_files[0], '/* This is file 1 */\n@import url("FiLe2.css")') helpers.create_file_with_content(paths_to_test_files[1], '/* This is LOCAL file 2 */') helpers.create_file_with_content(paths_to_test_files[2], '/* This is GLOBAL file 2 */') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths(Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual('/* This is file 1 */\n/* This is LOCAL file 2 */', actual_merged_content) helpers.clean_up_files(paths_to_test_files)
def test_merge_requirements_in_global_path(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.js'), os.path.join(self.test_env_dir, 'dir2', 'file2.js') ] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content( paths_to_test_files[0], '// This is file 1\n//= require FILE2') helpers.create_file_with_content(paths_to_test_files[1], '// This is file 2') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths( Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual('// This is file 1\n// This is file 2', actual_merged_content) helpers.clean_up_files(paths_to_test_files)
def tests_config_file_passed_as_an_argument_overrides_the_file_in_dot_blend( self): # Step 1: Assert the default configuration with no config files app = Application() self.assertTrue(app.config.analyzers, "expected the default analyzer list to be non-empty") self.assertTrue(app.config.minifiers, "expected the default minifier list to be non-empty") # Step 2: Assert that config is loaded from .blend/config.json clean_up_files(self.default_config_file_path) create_file_with_content(self.default_config_file_path, '{}') app = Application() self.assertIsNotNone(app.config) self.assertFalse(app.config.analyzers, "expected the analyzer list to be empty") self.assertFalse(app.config.minifiers, "expected the minifier list to be empty") # Step 3: Assert that a custom config file overrides .blend/config.json clean_up_files(self.test_config_file_path) create_file_with_content( self.test_config_file_path, """{ "analyzers": { "javascript": [ { "name": "blend.SizeAnalyzer", "skip_list": [ "bin" ] } ] } }""") app = Application(config_file_path=self.test_config_file_path) self.assertIsNotNone(app.config) self.assertEqual(1, len(app.config.analyzers), "expected one analyzer") self.assertFalse(app.config.minifiers, "expected the minifier list to be empty")
def test_merge_css_requirements_in_local_path(self): paths_to_test_files = [ os.path.join(self.test_env_dir, 'dir1', 'file1.css'), os.path.join(self.test_env_dir, 'dir1', 'file2.css'), os.path.join(self.test_env_dir, 'dir2', 'file2.css') ] helpers.clean_up_files(paths_to_test_files) helpers.create_file_with_content( paths_to_test_files[0], '/* This is file 1 */\n@import url("FiLe2.css")') helpers.create_file_with_content(paths_to_test_files[1], '/* This is LOCAL file 2 */') helpers.create_file_with_content(paths_to_test_files[2], '/* This is GLOBAL file 2 */') file1_resource = Resource(paths_to_test_files[0]) actual_merged_content = file1_resource.merge_requirements_from_paths( Paths(self.test_env_dir, include_cwd=False), previously_merged=[]) self.assertEqual('/* This is file 1 */\n/* This is LOCAL file 2 */', actual_merged_content) helpers.clean_up_files(paths_to_test_files)
def test_application_can_be_created_with_a_config_file_path(self): clean_up_files(self.test_config_file_path) create_file_with_content(self.test_config_file_path, '{}') Application(config_file_path=self.test_config_file_path)