def test_resource_has_a_size_property(self): test_env_dir = tempfile.mkdtemp() test_file_path = os.path.join(test_env_dir, 'test_file.js') helpers.create_file_with_content(test_file_path, '//some\ttext\non two lines') resource = Resource(test_file_path) self.assertEqual(24, resource.size) shutil.rmtree(test_env_dir)
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_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_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_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_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_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_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_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 test_can_load_minfiers_from_config_file(self): config_file_path = os.path.join(self.test_env_dir, 'blend.config') create_file_with_content(config_file_path, """{ "minifiers": { "javascript": { "name": "blend.YUICompressorMinifier" } } }""") conf = Configuration(config_file_path) resource = Resource('file.js') actual_minifier = conf.get_minifier_for_file_type(resource.file_type) self.assertIsNotNone(actual_minifier) self.assertIsInstance(actual_minifier, YUICompressorMinifier)
def test_can_load_minfiers_from_config_file(self): config_file_path = os.path.join(self.test_env_dir, 'blend.config') create_file_with_content( config_file_path, """{ "minifiers": { "javascript": { "name": "blend.YUICompressorMinifier" } } }""") conf = Configuration(config_file_path) resource = Resource('file.js') actual_minifier = conf.get_minifier_for_file_type(resource.file_type) self.assertIsNotNone(actual_minifier) self.assertIsInstance(actual_minifier, YUICompressorMinifier)
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_can_load_analyzers_from_config_file(self): config_file_path = os.path.join(self.test_env_dir, 'blend.config') create_file_with_content(config_file_path, """{ "analyzers": { "javascript": [ { "name": "blend.SizeAnalyzer", "skip_list": [ "bin" ] } ] } }""") conf = Configuration(config_file_path) resource = Resource('file.js') actual_analyzers = conf.get_analyzers_for_resource(resource) self.assertIsNotNone(actual_analyzers) self.assertEqual(1, len(actual_analyzers)) self.assertIsInstance(actual_analyzers[0], SizeAnalyzer) self.assertIsNotNone(conf.analyzer_skip_lists)
def test_can_load_analyzers_from_config_file(self): config_file_path = os.path.join(self.test_env_dir, 'blend.config') create_file_with_content( config_file_path, """{ "analyzers": { "javascript": [ { "name": "blend.SizeAnalyzer", "skip_list": [ "bin" ] } ] } }""") conf = Configuration(config_file_path) resource = Resource('file.js') actual_analyzers = conf.get_analyzers_for_resource(resource) self.assertIsNotNone(actual_analyzers) self.assertEqual(1, len(actual_analyzers)) self.assertIsInstance(actual_analyzers[0], SizeAnalyzer) self.assertIsNotNone(conf.analyzer_skip_lists)
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_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 make_a_minified_js_file(self, separator='.'): test_file_path = os.path.join(self.test_env_dir, 'test%smin.js' % separator) create_file_with_content(test_file_path, 'var a=42;') return test_file_path
def make_an_empty_js_file(self): test_file_path = os.path.join(self.test_env_dir, 'test.js') create_file_with_content(test_file_path, '') return test_file_path
def make_a_js_file(self, content='var answer = 42;'): test_file_path = os.path.join(self.test_env_dir, 'test.js') create_file_with_content(test_file_path, content) return test_file_path
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)
def setUp(self): self.analyzer = SizeAnalyzer() self.test_env_dir = tempfile.mkdtemp() self.test_file_path = os.path.join(self.test_env_dir, 'test_file.js') create_file_with_content(self.test_file_path, '//some\ttext\non two lines') self.resource = Resource(self.test_file_path)
def make_a_js_file_with_lint(self): test_file_path = os.path.join(self.test_env_dir, 'test.js') create_file_with_content(test_file_path, 'answer = 42;') # missing 'var' return test_file_path