Skip to content

Commit

Permalink
Modify tests to fix flakiness
Browse files Browse the repository at this point in the history
  • Loading branch information
blazyy authored and didix21 committed Oct 27, 2022
1 parent 780974b commit 5a70128
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
12 changes: 7 additions & 5 deletions mdutils/fileutils/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,35 @@ class MarkDownFile(object):
- Rewrite a file with new data.
- Write at the end of the file."""

def __init__(self, name=''):
def __init__(self, name='', tmp=''):
"""Creates a markdown file, if name is not empty.
:param str name: file name"""
import os
if name:
self.dirname = tmp if tmp else os.getcwd()
self.file_name = name if name.endswith('.md') else name + '.md'
self.file = open(self.file_name, 'w+', encoding='UTF-8')
self.file = open(f'{self.dirname}/{self.file_name}', 'w+', encoding='UTF-8')
self.file.close()

def rewrite_all_file(self, data):
"""Rewrite all the data of a Markdown file by ``data``.
:param str data: is a string containing all the data that is written in the markdown file."""
with open(self.file_name, 'w', encoding='utf-8') as self.file:
with open(f'{self.dirname}/{self.file_name}', 'w', encoding='utf-8') as self.file:
self.file.write(data)

def append_end(self, data):
"""Write at the last position of a Markdown file.
:param str data: is a string containing all the data that is written in the markdown file."""
with open(self.file_name, 'a', encoding='utf-8') as self.file:
with open(f'{self.dirname}/{self.file_name}', 'a', encoding='utf-8') as self.file:
self.file.write(data)

def append_after_second_line(self, data):
"""Write after the file's first line.
:param str data: is a string containing all the data that is written in the markdown file."""
with open(self.file_name, 'r+', encoding='utf-8') as self.file:
with open(f'{self.dirname}/{self.file_name}', 'r+', encoding='utf-8') as self.file:
file_data = self.file.read() # Save all the file's content
self.file.seek(0, 0) # Place file pointer at the beginning
first_line = self.file.readline() # Read the first line
Expand Down
29 changes: 11 additions & 18 deletions tests/test_fileutils/test_fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,53 @@ class TestMarkdownFile(TestCase):

def test_create_file(self):
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
file = MarkDownFile('Test_file')
file = MarkDownFile('Test_file', tmp)
self.assertEqual(file.file_name, 'Test_file.md')

def test_create_file_case_0(self):
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
file = MarkDownFile('Test_filemd')
file = MarkDownFile('Test_filemd', tmp)
self.assertEqual(file.file_name, 'Test_filemd.md')

def test_create_file_case_1(self):
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
file = MarkDownFile('Test_file.md')
file = MarkDownFile('Test_file.md', tmp)
self.assertEqual(file.file_name, 'Test_file.md')

def test_rewrite_all_file(self):
expected_content = "Write some content"
file_name = 'Test_file.md'
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
file = MarkDownFile(file_name)
file = MarkDownFile(file_name, tmp)
file.rewrite_all_file(expected_content)
with open(file_name, 'r') as actual_md:
with open(f'{tmp}/{file_name}', 'r') as actual_md:
self.assertEqual(actual_md.read(), expected_content)

def test_append_end(self):
expected_content = "Write some content and some data"
file_name = 'Test_file.md'
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
file = MarkDownFile(file_name)
file = MarkDownFile(file_name, tmp)
file.rewrite_all_file("Write some content")
file.append_end(" and some data")
with open(file_name, 'r') as actual_md:
with open(f'{tmp}/{file_name}', 'r') as actual_md:
self.assertEqual(actual_md.read(), expected_content)

def test_append_second_line(self):
expected_content = "This is the 1st line\nThis is the 2nd line\nThis is the 3th line\nThis is the 4th line"
file_name = 'Test_file.md'
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
file = MarkDownFile(file_name)
file = MarkDownFile(file_name, tmp)
file.rewrite_all_file("This is the 1st line\nThis is the 2nd line\nThis is the 4th line")
file.append_after_second_line("This is the 3th line")
with open(file_name, 'r') as actual_md:
with open(f'{tmp}/{file_name}', 'r') as actual_md:
self.assertEqual(actual_md.read(), expected_content)

def test_read(self):
expected_content = "This is the expected content after reading the file"
file_name = 'Test_file.md'
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
with open(file_name, 'w') as file:
with open(f'{tmp}/{file_name}', 'w') as file:
file.write(expected_content)

self.assertEqual(MarkDownFile.read_file(file_name), expected_content)
self.assertEqual(MarkDownFile.read_file(f'{tmp}/{file_name}'), expected_content)

0 comments on commit 5a70128

Please sign in to comment.