def test_newpy_overwriting_no(tmpdir): """ Run 'pytool newpy xyzzy' when xyzzy already exists. Verify that confirmation is requested. Answer 'no' and verify that the existing file is not overwritten. """ pytest.debug_func() xyzzy = toyfile(tmpdir, "xyzzy", content="original xyzzy\n") xyzzy_py = toyfile(tmpdir, "xyzzy.py", content="original xyzzy.py\n") with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") S = pexpect.spawn("{} newpy xyzzy".format(cmd)) which = S.expect([r'you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail('should have asked about overwriting xyzzy') expected = ['original xyzzy'] got = U.contents(xyzzy.strpath) assert got == expected expected = ['original xyzzy.py'] got = U.contents(xyzzy_py.strpath) assert got == expected
def test_editfile_empty(self): """ fl.editfile('emptyfile', 's', 'foo', 'bar', None) => rename emptyfile emptyfile.original, both empty """ fpath = U.pj(self.testdir, 'emptyfile') forig = fpath + ".original" U.touch(fpath) fl.editfile(fpath, 's', 'foo', 'bar', None) self.assertEq(True, U.exists(fpath)) self.assertEq(True, U.exists(forig)) self.assertEq([], U.contents(fpath)) self.assertEq([], U.contents(forig))
def test_newpy_overwriting_yes(tmpdir): """ Run 'pytool newpy xyzzy' when xyzzy, xyzzy.py already exist. Verify that confirmation is requested. Answer 'yes' and verify that the existing file IS overwritten. """ pytest.debug_func() xyzzy = toyfile(tmpdir, "xyzzy", content="original xyzzy") xyzzy_py = toyfile(tmpdir, "xyzzy.py", content="original xyzzy.py") with U.Chdir(tmpdir.strpath): cmd = pexpect.which('pytool') S = pexpect.spawn('%s newpy %s' % (cmd, xyzzy.strpath)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('yes') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail('should have asked about overwriting xyzzy') exp = os.path.abspath(xyzzy_py.strpath) got = os.readlink(xyzzy.strpath) assert got == exp expected = expected_xyzzy_py() got = U.contents(xyzzy_py.strpath) assert got == expected
def test_contents_empty(self): """ Calling contents on an empty file should return an empty list """ with U.Chdir(self.testdir): U.touch("empty_file") z = U.contents("empty_file") self.assertEqual([], z)
def test_contents_something(self): """ Calling contents on a non-empty file should return a list of lines form the file """ testdata = ["line 1", "line 2", "line 3"] with U.Chdir(self.testdir): th.write_file("something", content=testdata) z = U.contents("something") self.assertEqual(testdata, z)
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_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'])
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_newpy_prog_dir(tmpdir, fx_nopred): """ Run 'pytool.pt_newpy(**{'newpy': True, 'PROGRAM': 'xyzzy'})'. Verify that xyzzy and xyzzy.py are created and have the right contents. """ pytest.debug_func() pytool.pt_newpy(**{"newpy": True, "PROGRAM": "xyzzy", "d": False}) assert os.path.exists(fx_nopred.pname) exp = os.path.abspath(fx_nopred.pname) act = os.readlink(fx_nopred.lname) assert exp == act got = U.contents(fx_nopred.pname) exp = expected_xyzzy_py() assert got == exp
def test_newpy_prog_pxr(tmpdir, fx_nopred): """ Run 'pytool newpy xyzzy'. Verify that xyzzy and xyzzy.py are created and have the right contents. """ pytest.debug_func() r = pexpect.run("pytool newpy {}".format(fx_nopred.lname)) assert r == "" assert os.path.exists(fx_nopred.pname) exp = os.path.abspath(fx_nopred.pname) act = os.readlink(fx_nopred.lname) assert exp == act got = U.contents(fx_nopred.pname) exp = expected_xyzzy_py() assert got == exp