def test_check_outpath_isdir_fails(self): '''A cat where the outpath is currently a dir should fail checks.''' cat = Cat(Mock(spec=Templater)) cat.rules = [ Rule(self.srcfile1, self.tmpdir), Rule(self.srcfile2, self.tmpdir)] self.assertFalse(cat.check())
def test_check_srcpath_not_file_fails(self): '''A cat where one src file isn't readable should fail checks.''' cat = Cat(Mock(spec=Templater)) cat.rules = [ Rule(self.tmpdir / 'srcdne', self.tmpdir / 'outdne'), Rule(self.srcfile2, self.tmpdir / 'outdne')] self.assertFalse(cat.check())
def test_check_outpaths_dont_match_fails(self): '''A cat where the outpaths don't match should fail checks.''' cat = Cat(Mock(spec=Templater)) cat.rules = [ Rule(self.srcfile1, self.tmpdir / 'outdne'), Rule(self.srcfile2, self.tmpdir / 'differentdne')] self.assertFalse(cat.check())
def test_check_final_multiple_rules_fails(self): '''A cat with multiple rules should fail if any are final.''' cat = Cat(Mock(spec=Templater)) cat.rules = [ Rule(self.srcfile1, self.tmpdir / 'outdne', final=True), Rule(self.srcfile2, self.tmpdir)] self.assertFalse(cat.check())
def test_check_srcpaths_exist_outpath_dne_succeeds(self): '''A cat with readable srcs and nonexistent out should pass checks.''' cat = Cat(Mock(spec=Templater)) cat.rules = [ Rule(self.srcfile1, self.tmpdir / 'dne'), Rule(self.srcfile2, self.tmpdir / 'dne')] self.assertTrue(cat.check())
def test_deploy_differing_outpaths_raises(self): '''If deploying rules with differing outpaths, raise an error.''' cat = Cat(FakeTemplater( self.tmpdir, {})) cat.rules = [ Rule(self.srcfile1, self.outfile), Rule(self.srcfile2, self.tmpdir/'dne', force_rewrite=True)] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.outfile.write_text('theme: cooltheme\nforeground: white') with self.assertRaises(ImproperOutpathError): cat.deploy()
def test_deploy_file_dne_raises(self): '''A cat should raise an error if a src file dne.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) cat.rules = [ Rule(self.srcfile1, self.outfile), Rule(self.tmpdir/'dne', self.outfile)] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.outfile.write_text('some old garbage') with self.assertRaises(FileNotFoundError): cat.deploy()
def test_deploy_permissions_executable(self): '''The exec flag should cause the output file to be executable.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) cat.rules = [ Rule(self.srcfile1, self.outfile), Rule(self.srcfile2, self.outfile, executable=True)] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.srcfile2.write_text('') self.outfile.write_text('theme: cooltheme\nforeground: white') self.outfile.chmod(0o000) cat.deploy() self.assertEqual(0o100544, self.outfile.stat().st_mode)
def test_deploy_equal_files_flag_force_write(self): '''force_rewrite flag with equal output still writes the outfile.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) outpathspy = Mock(wraps=self.outfile) cat.rules = [ Rule(self.srcfile1, outpathspy), Rule(self.srcfile2, outpathspy, force_rewrite=True)] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.outfile.write_text('theme: cooltheme\nforeground: white') cat.deploy() # This is implementation-dependent -- look for a better option outpathspy.write_text.assert_called()
def test_deploy_outfile_dne_write(self): '''A cat that would change the content of the file should write it.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) cat.rules = [ Rule(self.srcfile1, self.tmpdir/'newoutfile'), Rule(self.srcfile2, self.tmpdir/'newoutfile')] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.srcfile2.write_text('background: {{ colorbg }}') cat.deploy() self.assertEqual( (self.tmpdir/'newoutfile').read_text(), 'theme: cooltheme\nforeground: white\nbackground: blue')
def test_get_rules(self): '''Should return basic rules from the module config.''' module_path = self.root / 'mod1' module_cfg_path = self.root / 'mod1' / 'module.yaml' module_path.mkdir() module_cfg_path.write_text('infile1.txt:\n' ' out: ~/outfile\n' 'infile2.txt:\n' ' out: ~/outfile2') rules = get_rules(module_path) self.assertEqual(rules, [ Rule(module_path / 'infile1.txt', Path('/fakehome/outfile')), Rule(module_path / 'infile2.txt', Path('/fakehome/outfile2')) ])
def test_deploy_file_empty_ignored(self): '''A cat should not add anything to the outfile for an empty src.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) cat.rules = [ Rule(self.srcfile1, self.outfile), Rule(self.srcfile2, self.outfile)] self.srcfile1.write_text('something cool\nnewline') self.srcfile2.write_text('') self.outfile.write_text('some old garbage') cat.deploy() self.assertEqual( self.outfile.read_text(), 'something cool\nnewline')
def test_get_rules_dir_contents(self): '''dir_contents flag should produce rules for all files in a subdir.''' module_path = self.root / 'mod1' module_cfg_path = self.root / 'mod1' / 'module.yaml' module_subdir_path = self.root / 'mod1' / 'subdir' module_path.mkdir() module_subdir_path.mkdir() (module_subdir_path / 'file1.txt').touch() (module_subdir_path / 'file2.txt').touch() module_cfg_path.write_text('subdir:\n' ' out: ~/outdir\n' ' dir_contents: true') rules = get_rules(module_path) self.assertCountEqual(rules, [ Rule(module_path / 'subdir' / 'file1.txt', Path('/fakehome/outdir/file1.txt')), Rule(module_path / 'subdir' / 'file2.txt', Path('/fakehome/outdir/file2.txt')) ])
def test_deploy_permissions_readonly(self): '''The output file should have readonly permissions.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) cat.rules = [Rule(self.srcfile1, self.outfile)] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.outfile.write_text('theme: cooltheme\nforeground: white') self.outfile.chmod(0o000) cat.deploy() self.assertEqual(0o100444, self.outfile.stat().st_mode)
def test_deploy_equal_files_no_write(self): '''A cat that would not change the outfile shouldn't open it.''' cat = Cat(FakeTemplater( self.tmpdir, {'theme': 'cooltheme', 'colorfg': 'white', 'colorbg': 'blue'})) outpathspy = Mock(wraps=self.outfile) cat.rules = [Rule(self.srcfile1, outpathspy)] self.srcfile1.write_text('theme: {{theme}}\nforeground: {{colorfg}}') self.outfile.write_text('theme: cooltheme\nforeground: white') cat.deploy() # This is implementation-dependent -- look for a better option outpathspy.write_text.assert_not_called()
def test_get_rules_with_flags(self): '''Should return a rule with all normal flags set from a config.''' module_path = self.root / 'mod1' module_cfg_path = self.root / 'mod1' / 'module.yaml' module_path.mkdir() module_cfg_path.write_text('infile1.txt:\n' ' out: ~/outfile\n' ' final: true\n' ' exec: true\n' ' force_rewrite: true') rules = get_rules(module_path) self.assertEqual(rules, [ Rule(module_path / 'infile1.txt', Path('/fakehome/outfile'), final=True, executable=True, force_rewrite=True) ])
def _rule_from_yaml(src: Path, out: Path, conf_dict: dict) -> Rule: return Rule(src, out, executable=conf_dict.get('exec', False), final=conf_dict.get('final', False), force_rewrite=conf_dict.get('force_rewrite', False))
def test_check_final_single_rule_succeeds(self): '''A cat with one final rule should pass checks.''' cat = Cat(Mock(spec=Templater)) cat.rules = [ Rule(self.srcfile1, self.tmpdir / 'outdne', final=True)] self.assertTrue(cat.check())
def test_bool_nonempty_istrue(self): '''A cat object with a rule added should return false.''' cat = Cat(Mock(spec=Templater)) cat.rules = [Rule(Path(), Path())] self.assertTrue(cat)
def test_str_nonempty_exists(self): '''A non-empty cat object should have a string representation.''' cat = Cat(Mock(spec=Templater)) cat.rules = [Rule(Path(), Path())] self.assertTrue(str(cat))