def test_read_file_lines(self): path = self.temp_path('a/b/c.txt') lines = [ '', '1 ', ' 2', '', '', ' 3 ', ' 4 ', '', '', '5', ] fsutil.write_file(path, content='\n'.join(lines)) expected_lines = list(lines) lines = fsutil.read_file_lines(path, strip_white=False, skip_empty=False) self.assertEqual(lines, expected_lines) expected_lines = [ '', '1', '2', '', '', '3', '4', '', '', '5', ] lines = fsutil.read_file_lines(path, strip_white=True, skip_empty=False) self.assertEqual(lines, expected_lines) expected_lines = [ '1 ', ' 2', ' 3 ', ' 4 ', '5', ] lines = fsutil.read_file_lines(path, strip_white=False, skip_empty=True) self.assertEqual(lines, expected_lines) expected_lines = [ '1', '2', '3', '4', '5', ] lines = fsutil.read_file_lines(path, strip_white=True, skip_empty=True) self.assertEqual(lines, expected_lines)
def test_write_file_with_append(self): path = self.temp_path("a/b/c.txt") fsutil.write_file(self.temp_path("a/b/c.txt"), content="Hello World") self.assertEqual(fsutil.read_file(path), "Hello World") fsutil.write_file( self.temp_path("a/b/c.txt"), content=" - Hello Sun", append=True ) self.assertEqual(fsutil.read_file(path), "Hello World - Hello Sun")
def test_write_file_with_append(self): path = self.temp_path('a/b/c.txt') fsutil.write_file(self.temp_path('a/b/c.txt'), content='Hello World') self.assertEqual(fsutil.read_file(path), 'Hello World') fsutil.write_file(self.temp_path('a/b/c.txt'), content=' - Hello Sun', append=True) self.assertEqual(fsutil.read_file(path), 'Hello World - Hello Sun')
def test_get_file_last_modified_date(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(path, content="Hello") creation_date = fsutil.get_file_creation_date(path) time.sleep(0.2) fsutil.write_file(path, content="Goodbye", append=True) now = dt.datetime.now() lastmod_date = fsutil.get_file_last_modified_date(path) self.assertTrue((now - lastmod_date) < dt.timedelta(seconds=0.1)) self.assertTrue((lastmod_date - creation_date) > dt.timedelta(seconds=0.15))
def test_read_file_lines(self): path = self.temp_path("a/b/c.txt") lines = ["", "1 ", " 2", "", "", " 3 ", " 4 ", "", "", "5"] fsutil.write_file(path, content="\n".join(lines)) expected_lines = list(lines) lines = fsutil.read_file_lines(path, strip_white=False, skip_empty=False) self.assertEqual(lines, expected_lines) expected_lines = ["", "1", "2", "", "", "3", "4", "", "", "5"] lines = fsutil.read_file_lines(path, strip_white=True, skip_empty=False) self.assertEqual(lines, expected_lines) expected_lines = ["1 ", " 2", " 3 ", " 4 ", "5"] lines = fsutil.read_file_lines(path, strip_white=False, skip_empty=True) self.assertEqual(lines, expected_lines) expected_lines = ["1", "2", "3", "4", "5"] lines = fsutil.read_file_lines(path, strip_white=True, skip_empty=True) self.assertEqual(lines, expected_lines)
def write_file(filepath, content, **options): fsutil.write_file(filepath, content, **options)
def test_read_file(self): path = self.temp_path("a/b/c.txt") fsutil.write_file(path, content="Hello World") self.assertEqual(fsutil.read_file(path), "Hello World")
def test_write_file(self): path = self.temp_path('a/b/c.txt') fsutil.write_file(self.temp_path('a/b/c.txt'), content='Hello World') self.assertEqual(fsutil.read_file(path), 'Hello World') fsutil.write_file(self.temp_path('a/b/c.txt'), content='Hello Jupiter') self.assertEqual(fsutil.read_file(path), 'Hello Jupiter')