Exemple #1
0
		def test(suffixes, next_suffix):
			textfile = TextFile(self.test_filename, backup=True)
			create_files(suffixes)
			backup_filename = textfile.backup()
			self.assertEqual(backup_filename,  self.test_filename + '.' + TextFile.default_backup_ext + next_suffix)
			self.assertTrue(exists(backup_filename))
			remove_files(suffixes + [next_suffix])
Exemple #2
0
	def test_file_creation(self):
		filename = 'test.txt'
		textfile = TextFile(filename)

		textfile.append_line(self.line)
		self.assertTrue(exists(filename))

		os.remove(filename)
Exemple #3
0
	def test_insert_line_after(self):
		textfile = TextFile(self.test_filename)
		line = '1234567890'

		count = textfile.insert_line_after(line, contains='BASH_VERSION')
		
		self.assertEqual(1, count)
		with open(self.test_filename, 'r') as f:
			lines = f.read().splitlines()
			self.assertEqual(lines[3], self.test_file.splitlines()[3])
			self.assertEqual(lines[4], line)
Exemple #4
0
	def test_find_lines(self):
		textfile = TextFile(self.test_filename)

		self.assertEqual(0, textfile.find_lines(self.id))

		textfile.append_line(self.line, id=self.id)
		self.assertEqual(1, textfile.find_lines(self.id))

		self.append_line_multiple_times(textfile, 4)
		self.assertEqual(5, textfile.find_lines(self.id))

		textfile.remove_lines(self.id)
		self.assertEqual(0, textfile.find_lines(self.id))
Exemple #5
0
	def test_remove_dups(self):
		textfile = TextFile(self.test_filename)

		self.append_line_multiple_times(textfile, 4)
		textfile.append_line(self.line, id=self.id, remove_dups=True)

		self.assertEqual(1, textfile.find_lines(self.id))

		textfile.append_line(self.line, id=self.id, remove_dups=True)
		self.assertEqual(1, textfile.find_lines(self.id))
Exemple #6
0
	def test_remove_lines(self):
		filename = 'test.txt'

		with open(filename, 'w') as f:
			for i in range(0, 100):
				f.write('even' if (i % 2 == 0) else 'odd')
			f.write('other')
			f.write('and more text')

		textfile = TextFile(filename)
		textfile.remove_lines(startswith='even', line_no=100, contains='more')

		with open(filename, 'r') as f:
			lines = f.read().splitlines()
			for line in lines:
				self.assertEqual('odd', line)

		os.remove(filename)
Exemple #7
0
	def test_file_not_found(self):
		tf = TextFile(self.noexist_filename)

		with self.assertRaises(TextFileError) as e:
			tf.backup()

		with self.assertRaises(TextFileError) as e:
			tf.insert_line_before('123', startswith='')
Exemple #8
0
	def test_remove_from_empty_file(self):
		filename = self.empty_filename

		tf = TextFile(filename)

		tf.remove_lines(self.id)
		self.assertEquals(open(filename, 'r').read(), '')

		tf.remove_section(self.id)
		self.assertEquals(open(filename, 'r').read(), '')
Exemple #9
0
	def test_remove_nested_section(self):
		tf = TextFile(self.test_filename)

		with open(self.test_filename, 'a+') as f:
			f.write(tf._comment_prefix + tf.section_start_prefix + self.id + os.linesep)

		tf.append_section(self.section, id=self.id)

		with self.assertRaises(TextFileError) as a:
			tf.remove_section(self.id)
Exemple #10
0
	def test_remove_section_with_no_end_marker(self):
		filename = self.test_filename

		tf = TextFile(filename)
		self.print_file(filename, "original file")

		tf.append_section(self.section, id=self.id)
		self.print_file(filename, "file after appending section")

		self.remove_last_line_from_file(self.test_filename)
		self.print_file(filename, "file after removing end marker of section")

		with self.assertRaises(TextFileError) as a:
			tf.remove_section(self.id)
Exemple #11
0
	def append_remove_lines(self, filename, mod_after_append=False):
		self.print_file(filename, "original file")
		tf = TextFile(filename)

		orig_text = open(filename, 'r').read()
		tf.append_line(self.line, id=self.id)
		self.print_file(filename, "file after appending line")

		if mod_after_append:
			self.append_to_file(filename, self.more1)

		with open(filename, 'r') as f:
			self.assertNotEquals(f.read().find(self.line), -1)

		tf.remove_lines(self.id)
		self.print_file(filename, "file after removing appended line")

		with open(filename, 'r') as f:
			text = f.read()
			self.assertEquals(text.find(self.line), -1)
			if not mod_after_append:
				self.assertEquals(text, orig_text)
Exemple #12
0
	def test_backup_creation(self):
		textfile = TextFile(self.test_filename, backup=True)
		textfile.remove_lines(line_no=0)

		self.assertTrue(exists(self.test_filename + '.' + TextFile.default_backup_ext))