def test_diff(self): """ Test diff """ with util.Chdir("fl_tests"): util.writefile('mrpm1', ['this is a test file\n']) util.writefile('mrpm2', ['this is another test file\n']) util.writefile('mrpm1.2009-10-01', ['copy of test file\n']) util.writefile('old/mrpm2.2009-08-31', ['copy of another test file\n']) expected = ['diff ./mrpm1.2009-10-01 mrpm1\r', '1c1\r', '< copy of test file\r', '---\r', '> this is a test file\r', ''] cmd = util.script_location("fl") got = pexpect.run("%s diff mrpm1" % cmd).split("\n") self.assertEqual(expected, got) expected = ['diff ./old/mrpm2.2009-08-31 mrpm2\r', '1c1\r', '< copy of another test file\r', '---\r', '> this is another test file\r', ''] got = pexpect.run("%s diff mrpm2" % cmd).split("\n") self.assertEqual(expected, got)
def test_most_recent_prefix_match(self): """ Test the routine most_recent_prefix_match() """ with util.Chdir("fl_tests"): util.writefile('mrpm1', ['this is a test file']) util.writefile('mrpm2', ['this is another test file']) os.system('cp mrpm1 mrpm1.2009-10-01') os.system('cp mrpm2 old/mrpm2.2009-08-31') a = fl.most_recent_prefix_match('.', 'mrpm1') self.assertEq('./mrpm1.2009-10-01', a) a = fl.most_recent_prefix_match('.', 'mrpm2') self.assertEq('./old/mrpm2.2009-08-31', a)
def test_batch_command_dryrun(tmpdir, capsys): """ Test batch_command with dryrun True and quiet False. """ pytest.debug_func() with U.Chdir(tmpdir.strpath): U.writefile('tmpfile', [str(x) + '\n' for x in range(1, 250)]) v = optparse.Values({'dryrun': True, 'quiet': False, 'xargs': True, 'cmd': 'echo %'}) with open('tmpfile', 'r') as f: q = fx.xargs_wrap("echo %", f) exp = '' for chunk in q: exp += re.sub('^echo ', "would do 'echo ", chunk) + "'\n" with open("tmpfile", 'r') as f: fx.batch_command(v, [], f) assert exp in "".join(capsys.readouterr())
def test_editfile_delete(self): """ fl.editfile('legit', 's', 'foo', '', None) """ fp = U.pj(self.testdir, U.function_name()) forig = fp + ".original" tdata = ["foo bar", "bar foo", "barfoo", "foobar foo", "loofafool"] xdata = [re.sub('^foo', '', z) for z in tdata] U.writefile(fp, tdata, newlines=True) fl.editfile(fp, 's', '^foo', '', None) self.assertEq(True, U.exists(fp)) self.assertEq(True, U.exists(forig)) self.assertEq(xdata, U.contents(fp)) self.assertEq(tdata, U.contents(forig))
def test_editfile_suffix(self): """ fl.editfile('legit', 's', 'foo', 'bar', 'old') """ fp = U.pj(self.testdir, U.function_name()) forig = fp + ".old" tdata = ["foo bar", "bar foo", "barfoo", "foobar foo", "loofafool"] xdata = [z.replace('foo', 'bar') for z in tdata] U.writefile(fp, tdata, newlines=True) fl.editfile(fp, 's', 'foo', 'bar', 'old') self.assertEq(True, U.exists(fp)) self.assertEq(True, U.exists(forig)) self.assertEq(xdata, U.contents(fp)) self.assertEq(tdata, U.contents(forig))
def test_editfile_legit(self): """ fl.editfile('legit', 's', 'foo', 'bar', None) """ fp = U.pj(self.testdir, 'legit') forig = fp + ".original" tdata = ["foo bar", "bar foo", "barfoo", "foobar foo", "loofafool"] xdata = [z.replace('foo', 'bar') for z in tdata] U.writefile(fp, tdata, newlines=True) fl.editfile(fp, 's', 'foo', 'bar', None) self.assertEq(True, U.exists(fp)) self.assertEq(True, U.exists(forig)) self.assertEq(xdata, U.contents(fp)) self.assertEq(tdata, U.contents(forig))
def test_BatchCommand_dryrun(self): """ Test BatchCommand with dryrun True and quiet False. """ with U.Chdir(self.testdir): U.writefile('tmpfile', [str(x) + '\n' for x in range(1, 250)]) v = optparse.Values({'dryrun': True, 'quiet': False, 'xargs': True, 'cmd': 'echo %'}) with open('tmpfile', 'r') as f: q = fx.xargs_wrap("echo %", f) exp = '' for chunk in q: exp += re.sub('^echo ', "would do 'echo ", chunk) + "'\n" with th.StdoutExcursion() as getval: with open('tmpfile', 'r') as f: fx.BatchCommand(v, [], f) actual = getval() self.assertEq(exp, actual)
def fl_edit_ok(self, eopt='', iopt='', files=0, inp=[], exp=[]): """ Common code for all the test_fl_edit_* routines """ fl = [] for idx in range(files): (fd, fp) = tempfile.mkstemp(dir=self.testdir) os.close(fd) fl.append(fp) util.writefile(fp, inp) suffix = 'original' with util.Chdir(self.testdir): cmd = "fl edit " if eopt != '': cmd += '-e "%s" ' % eopt if iopt != '': cmd += '-i %s ' % iopt suffix = iopt if 0 < len(fl): cmd += " ".join(fl) result = pexpect.run(cmd) self.assertTrue(result == "", "Expected '%s' to be empty" % result) for fp in fl: forig = fp + "." + suffix self.assertTrue(util.exists(forig), "Expected %s to exist" % forig) self.assertTrue("foo four five foo" in util.contents(forig), "Contents of %s have changed" % forig) self.assertTrue(util.exists(fp), "Expected %s to exist" % fp) self.assertEq(exp, util.contents(fp)) for fp in fl: util.safe_unlink(fp)
def test_revert(self): """ Test revert """ with util.Chdir("fl_tests"): util.writefile('mrpm1', ['this is a test file\n']) util.writefile('mrpm2', ['this is another test file\n']) util.writefile('mrpm1.2009-10-01', ['reverted\n']) util.writefile('old/mrpm2.2009-08-31', ['REVERTED\n']) fl = util.script_location("fl") os.system('%s revert mrpm1' % fl) assert(os.path.exists('mrpm1.new')) assert(os.path.exists('mrpm1')) assert(not os.path.exists('mrpm1.2009-10-01')) assert(util.contents('mrpm1') == ['reverted']) os.system('%s revert mrpm2' % fl) assert(os.path.exists('mrpm2.new')) assert(os.path.exists('mrpm2')) assert(not os.path.exists('old/mrpm2.2009-08-31')) assert(util.contents('mrpm2') == ['REVERTED'])