Skip to content

Commit

Permalink
Fix #81: Absolute path are broken
Browse files Browse the repository at this point in the history
- Add new test for improving coverage.
- Install pytest on github actions.
- Replace unittest for pytest on github actions.
  • Loading branch information
didix21 committed Mar 11, 2023
1 parent 5c71356 commit e361168
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -21,10 +21,12 @@ jobs:
with:
python-version: '3.6'
- name: Install dependencies
run: pip install coverage
run: |
pip install coverage
pip install -U pytest
- name: Test unittest
run: |
coverage run -m unittest discover
coverage run -m pytest
coverage xml
- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v2
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
27 changes: 17 additions & 10 deletions mdutils/fileutils/fileutils.py
Expand Up @@ -15,35 +15,42 @@ class MarkDownFile(object):
- Rewrite a file with new data.
- Write at the end of the file."""

def __init__(self, name='', tmp=''):
def __init__(self, name='', dirname: str = None):
"""Creates a markdown file, if name is not empty.
:param str name: file name"""
import os
:param str name: provide the file name or a path joinly with the file name. For example: `./my-path/my-file.md`.
:param str dirname: use dirname if you want to provide the path separately from the name.
If not you can specify the path directly on the name."""
if name:
self.dirname = tmp if tmp else os.getcwd()
self.file_name = name if name.endswith('.md') else name + '.md'
self.file = open(f'{self.dirname}/{self.file_name}', 'w+', encoding='UTF-8')
self.dirname = dirname
self.file_name = self._get_file_name(name, dirname)
self.file = open(f'{self.file_name}', 'w+', encoding='UTF-8')
self.file.close()

def _get_file_name(self, name: str, dirname: str = None ) -> str:
if dirname:
return f'{dirname}/{name}' if name.endswith('.md') else f'{dirname}/{name}.md'

return name if name.endswith('.md') else f'{name}.md'

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(f'{self.dirname}/{self.file_name}', 'w', encoding='utf-8') as self.file:
with open(f'{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(f'{self.dirname}/{self.file_name}', 'a', encoding='utf-8') as self.file:
with open(f'{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(f'{self.dirname}/{self.file_name}', 'r+', encoding='utf-8') as self.file:
with open(f'{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 All @@ -53,7 +60,7 @@ def append_after_second_line(self, data):
self.file.write('\n' + file_data[len(first_line + second_line):])

@staticmethod
def read_file(file_name):
def read_file(file_name: str) -> str:
"""Read a Markdown file using a file name. It is not necessary to add *.md extension.
:param file_name: Markdown file's name.
Expand Down
12 changes: 8 additions & 4 deletions tests/test_fileutils/test_fileutils.py
Expand Up @@ -9,25 +9,29 @@
from unittest import TestCase
from mdutils.fileutils import MarkDownFile
import tempfile
import os


class TestMarkdownFile(TestCase):

def test_create_file(self):
with tempfile.TemporaryDirectory() as tmp:
file = MarkDownFile('Test_file', tmp)
self.assertEqual(file.file_name, 'Test_file.md')
self.assertEqual(file.file_name, f'{tmp}/Test_file.md')

def test_create_file_case_0(self):
with tempfile.TemporaryDirectory() as tmp:
file = MarkDownFile('Test_filemd', tmp)
self.assertEqual(file.file_name, 'Test_filemd.md')
self.assertEqual(file.file_name, f'{tmp}/Test_filemd.md')

def test_create_file_case_1(self):
with tempfile.TemporaryDirectory() as tmp:
file = MarkDownFile('Test_file.md', tmp)
self.assertEqual(file.file_name, 'Test_file.md')
self.assertEqual(file.file_name, f'{tmp}/Test_file.md')

def test_create_file_case_3(self):
with tempfile.TemporaryDirectory() as tmp:
file = MarkDownFile(f'{tmp}/Test_file.md')
self.assertEqual(file.file_name, f'{tmp}/Test_file.md')

def test_rewrite_all_file(self):
expected_content = "Write some content"
Expand Down

0 comments on commit e361168

Please sign in to comment.