Skip to content

Commit

Permalink
Updated tests to run independently of user instance of app.
Browse files Browse the repository at this point in the history
Tests updated so that they are running with a temporary folder as a config folder which is deleted after the tests are executed (either successfully or not). The test data are not mixed with user data of a developer.

Refers to the following issue:
yoda-pa#116

Changes
- a function is executed prior to each test which creates a temporary folder and changes the yoda config path to this folder
- a function is executed after each test which deletes the folder and restores the original yoda config path
- therefore, all constants refering to file paths (in all modules) have been changed to functions, so that they are able to react to config path changes (otherwise it could result in a weird behaviour)
(changing file path constants to dynamically resolved functions also helps in cases where two instances of yoda are run simultaneously and one of them changes the config path)
  • Loading branch information
zuziik committed Jun 18, 2018
1 parent 9427db5 commit 626f768
Show file tree
Hide file tree
Showing 25 changed files with 542 additions and 164 deletions.
21 changes: 12 additions & 9 deletions modules/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
from .config import get_config_file_paths
from .util import get_folder_path_from_file_path, create_folder

# config paths
def get_alias_config_file_path():
return get_config_file_paths()["ALIAS_CONFIG_FILE_PATH"]

ALIAS_CONFIG_FILE_PATH = get_config_file_paths()["ALIAS_CONFIG_FILE_PATH"]
ALIAS_CONFIG_FOLDER_PATH = get_folder_path_from_file_path(ALIAS_CONFIG_FILE_PATH)
def get_alias_config_folder_path():
return get_folder_path_from_file_path(get_alias_config_file_path())

class Alias(click.Group):

_aliases = {}

def __init__(self, *args, **kwargs):
create_folder(ALIAS_CONFIG_FOLDER_PATH)
create_folder(get_alias_config_folder_path())
try:
with open(ALIAS_CONFIG_FOLDER_PATH + '/alias.txt', 'r') as f:
with open(get_alias_config_folder_path() + '/alias.txt', 'r') as f:
lines = f.readlines()
for i in range(1, len(lines), 2):
Alias._aliases[lines[i].strip('\n')] = lines[i - 1].strip('\n').split()
except:
fo = open(ALIAS_CONFIG_FOLDER_PATH + '/alias.txt', 'w')
fo = open(get_alias_config_folder_path() + '/alias.txt', 'w')
fo.close()
super(Alias, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -90,8 +93,8 @@ def new(orig_cmd, alias_cmd):
click.echo("Aliasing failed - Alias name already exists. Use alias delete to remove it")
return
if orig_cmd and alias_cmd:
create_folder(ALIAS_CONFIG_FOLDER_PATH)
with open(ALIAS_CONFIG_FOLDER_PATH + '/alias.txt', 'a') as f:
create_folder(get_alias_config_folder_path())
with open(get_alias_config_folder_path() + '/alias.txt', 'a') as f:
f.write(orig_cmd + '\n' + alias_cmd + '\n')
Alias._aliases[alias_cmd] = orig_cmd
click.echo("Aliased %s as %s" % (orig_cmd, alias_cmd))
Expand All @@ -107,8 +110,8 @@ def delete(alias):
click.echo("Alias delete failed - Could not find alias")
return
del Alias._aliases[alias]
create_folder(ALIAS_CONFIG_FOLDER_PATH)
with open(ALIAS_CONFIG_FOLDER_PATH + '/alias.txt', 'w') as f:
create_folder(get_alias_config_folder_path())
with open(get_alias_config_folder_path() + '/alias.txt', 'w') as f:
for key in Alias._aliases.keys():
f.write(' '.join(Alias._aliases[key]) + '\n' + key + '\n')

Expand Down
7 changes: 7 additions & 0 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

DEFAULT_CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.yoda')

def get_config_folder():
"""
Gets the absolute path of the yoda config folder
"""
with open(YODA_CONFIG_FILE_PATH, 'r') as config_file:
config_folder = config_file.read()
return config_folder

def update_config_path(new_path):
"""
Expand Down
85 changes: 42 additions & 43 deletions modules/diary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,58 @@
from .util import *

# config file path
DIARY_CONFIG_FILE_PATH = get_config_file_paths()['DIARY_CONFIG_FILE_PATH']
DIARY_CONFIG_FOLDER_PATH = get_folder_path_from_file_path(
DIARY_CONFIG_FILE_PATH)
def get_diary_config_file_path():
return get_config_file_paths()['DIARY_CONFIG_FILE_PATH']

def get_diary_config_folder_path():
return get_folder_path_from_file_path(
get_diary_config_file_path())

def now_time():
def get_todays_tasks_entry_file_path():
"""
get time
get file path for today's tasks entry file
:return:
"""
return str(time.strftime("%H:%M:%S"))
return get_diary_config_folder_path() + '/' + now_date() + "-tasks.yaml"


def now_date():
def get_todays_notes_entry_file_path():
"""
get date
get file path for today's notes entry file
:return:
"""
return str(time.strftime("%d-%m-%Y"))

return get_diary_config_folder_path() + '/' + now_date() + "-notes.yaml"

def todays_tasks_entry_file_path():
def get_tasks_entry_file_path(date):
"""
get file path for today's tasks entry file
:return:
get file path for the specified date's notes entry file
"""
return DIARY_CONFIG_FOLDER_PATH + '/' + now_date() + "-tasks.yaml"
return get_diary_config_folder_path() + '/' + date + "-tasks.yaml"

#------------------------------------------------------------------------------

def todays_notes_entry_file_path():
def now_time():
"""
get file path for today's notes entry file
get time
:return:
"""
return DIARY_CONFIG_FOLDER_PATH + '/' + now_date() + "-notes.yaml"
return str(time.strftime("%H:%M:%S"))


def tasks_entry_file_path(date):
def now_date():
"""
get file path for the specified date's notes entry file
get date
:return:
"""
return DIARY_CONFIG_FOLDER_PATH + '/' + date + "-tasks.yaml"

TODAYS_TASKS_ENTRY_FILE_PATH = todays_tasks_entry_file_path()
TODAYS_NOTES_ENTRY_FILE_PATH = todays_notes_entry_file_path()

return str(time.strftime("%d-%m-%Y"))

def today_entry_check():
"""
check if today's diary entry file exists. If not, create
"""
if not os.path.exists(DIARY_CONFIG_FOLDER_PATH):
if not os.path.exists(get_diary_config_folder_path()):
try:
os.makedirs(DIARY_CONFIG_FOLDER_PATH)
os.makedirs(get_diary_config_folder_path())
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
Expand Down Expand Up @@ -125,14 +124,14 @@ def new_task():
click.echo(chalk.red(
'Invalid goal name'))

if os.path.isfile(TODAYS_TASKS_ENTRY_FILE_PATH):
if os.path.isfile(get_todays_tasks_entry_file_path()):
setup_data = dict(
time=timestamp,
text=note,
status=0,
hashtags=" ".join(hashtags)
)
append_data_into_file(setup_data, TODAYS_TASKS_ENTRY_FILE_PATH)
append_data_into_file(setup_data, get_todays_tasks_entry_file_path())
else:
setup_data = dict(
entries=[
Expand All @@ -144,7 +143,7 @@ def new_task():
)
]
)
input_data(setup_data, TODAYS_TASKS_ENTRY_FILE_PATH)
input_data(setup_data, get_todays_tasks_entry_file_path())


def new_note():
Expand All @@ -156,13 +155,13 @@ def new_note():
click.echo(chalk.blue('Input your entry for note:'))
note = input().strip()

if os.path.isfile(TODAYS_NOTES_ENTRY_FILE_PATH):
with open(TODAYS_NOTES_ENTRY_FILE_PATH) as todays_notes_entry:
if os.path.isfile(get_todays_notes_entry_file_path()):
with open(get_todays_notes_entry_file_path()) as todays_notes_entry:
setup_data = dict(
time=now_time(),
text=note
)
append_data_into_file(setup_data, TODAYS_NOTES_ENTRY_FILE_PATH)
append_data_into_file(setup_data, get_todays_notes_entry_file_path())
else:
setup_data = dict(
entries=[
Expand All @@ -172,7 +171,7 @@ def new_note():
)
]
)
input_data(setup_data, TODAYS_NOTES_ENTRY_FILE_PATH)
input_data(setup_data, get_todays_notes_entry_file_path())


def strike(text):
Expand All @@ -188,14 +187,14 @@ def tasks():
"""
get tasks
"""
if os.path.isfile(TODAYS_TASKS_ENTRY_FILE_PATH):
if os.path.isfile(get_todays_tasks_entry_file_path()):
click.echo('Today\'s agenda:')
click.echo('----------------')
click.echo("Status | Time | Text")
click.echo("-------|---------|-----")
incomplete_tasks = 0
total_tasks = 0
with open(TODAYS_TASKS_ENTRY_FILE_PATH) as todays_tasks_entry:
with open(get_todays_tasks_entry_file_path()) as todays_tasks_entry:
contents = yaml.load(todays_tasks_entry)
for entry in contents['entries']:
total_tasks += 1
Expand Down Expand Up @@ -227,8 +226,8 @@ def complete_task():
complete a task
"""
not_valid_task_number = 1
if os.path.isfile(TODAYS_TASKS_ENTRY_FILE_PATH):
with open(TODAYS_TASKS_ENTRY_FILE_PATH) as todays_tasks_entry:
if os.path.isfile(get_todays_tasks_entry_file_path()):
with open(get_todays_tasks_entry_file_path()) as todays_tasks_entry:
contents = yaml.load(todays_tasks_entry)
i = 0
no_task_left = True
Expand Down Expand Up @@ -263,7 +262,7 @@ def complete_task():
click.echo(chalk.red('Please Enter a valid task number!'))
else:
contents['entries'][task_to_be_completed - 1]['status'] = 1
input_data(contents, TODAYS_TASKS_ENTRY_FILE_PATH)
input_data(contents, get_todays_tasks_entry_file_path())
not_valid_task_number = 0
else:
click.echo(chalk.red(
Expand All @@ -274,8 +273,8 @@ def notes():
"""
see notes for today
"""
if os.path.isfile(TODAYS_NOTES_ENTRY_FILE_PATH):
with open(TODAYS_NOTES_ENTRY_FILE_PATH) as todays_notes_entry:
if os.path.isfile(get_todays_notes_entry_file_path()):
with open(get_todays_notes_entry_file_path()) as todays_notes_entry:
contents = yaml.load(todays_notes_entry)

click.echo('Today\'s notes:')
Expand Down Expand Up @@ -330,7 +329,7 @@ def list_of_tasks_files():
"""
current_month = time.strftime("%m")
current_year = time.strftime("%Y")
files = [f for f in listdir(DIARY_CONFIG_FOLDER_PATH) if os.path.isfile(os.path.join(DIARY_CONFIG_FOLDER_PATH, f))]
files = [f for f in listdir(get_diary_config_folder_path()) if os.path.isfile(os.path.join(get_diary_config_folder_path(), f))]
list_of_files = []
for i in files:
x = i[3:16].split('-')
Expand All @@ -349,7 +348,7 @@ def current_month_task_analysis():
total_incomplete_tasks = 0
list_of_files = list_of_tasks_files()
for some_file in range(0, len(list_of_files)):
list_of_files[some_file] = os.path.join(DIARY_CONFIG_FOLDER_PATH, list_of_files[some_file])
list_of_files[some_file] = os.path.join(get_diary_config_folder_path(), list_of_files[some_file])
for some_file in list_of_files:
with open(some_file) as fp:
contents = yaml.load(fp)
Expand All @@ -370,7 +369,7 @@ def current_month_task_analysis():


def get_task_info(timestamp, date):
filename = tasks_entry_file_path(date)
filename = get_tasks_entry_file_path(date)
if os.path.isfile(filename):
with open(filename) as file:
contents = yaml.load(file)
Expand Down
36 changes: 19 additions & 17 deletions modules/goals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from .util import *

# config file path
GOALS_CONFIG_FILE_PATH = get_config_file_paths()['GOALS_CONFIG_FILE_PATH']
GOALS_CONFIG_FOLDER_PATH = get_folder_path_from_file_path(
GOALS_CONFIG_FILE_PATH)
def get_goals_config_file_path():
return get_config_file_paths()['GOALS_CONFIG_FILE_PATH']

def get_goals_config_folder_path():
return get_folder_path_from_file_path(
get_goals_config_file_path())

def strike(text):
"""
Expand All @@ -20,7 +22,7 @@ def strike(text):
return u'\u0336'.join(text) + u'\u0336'

def get_goal_file_path(goal_name):
return GOALS_CONFIG_FOLDER_PATH + '/' + goal_name + '.yaml'
return get_goals_config_folder_path() + '/' + goal_name + '.yaml'

def process(input):
"""
Expand Down Expand Up @@ -53,9 +55,9 @@ def goals_dir_check():
"""
check if goals directory exists. If not, create
"""
if not os.path.exists(GOALS_CONFIG_FOLDER_PATH):
if not os.path.exists(get_goals_config_folder_path()):
try:
os.makedirs(GOALS_CONFIG_FOLDER_PATH)
os.makedirs(get_goals_config_folder_path())
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
Expand Down Expand Up @@ -83,8 +85,8 @@ def complete_goal():
complete a goal
"""
not_valid_goal_number = 1
if os.path.isfile(GOALS_CONFIG_FILE_PATH):
with open(GOALS_CONFIG_FILE_PATH) as todays_tasks_entry:
if os.path.isfile(get_goals_config_file_path()):
with open(get_goals_config_file_path()) as todays_tasks_entry:
contents = yaml.load(todays_tasks_entry)
i = 0
no_goal_left = True
Expand Down Expand Up @@ -119,7 +121,7 @@ def complete_goal():
click.echo(chalk.red('Please Enter a valid goal number!'))
else:
contents['entries'][goal_to_be_completed - 1]['status'] = 1
input_data(contents, GOALS_CONFIG_FILE_PATH)
input_data(contents, get_goals_config_file_path())
not_valid_goal_number = 0
else:
click.echo(chalk.red(
Expand Down Expand Up @@ -165,14 +167,14 @@ def new_goal():
except ValueError:
click.echo(chalk.red("Incorrect data format, should be YYYY-MM-DD. Please repeat:"))

if os.path.isfile(GOALS_CONFIG_FILE_PATH):
if os.path.isfile(get_goals_config_file_path()):
setup_data = dict(
name=goal_name,
text=text,
deadline=deadline,
status=0
)
append_data_into_file(setup_data, GOALS_CONFIG_FILE_PATH)
append_data_into_file(setup_data, get_goals_config_file_path())
else:
setup_data = dict(
entries=[
Expand All @@ -184,7 +186,7 @@ def new_goal():
)
]
)
input_data(setup_data, GOALS_CONFIG_FILE_PATH)
input_data(setup_data, get_goals_config_file_path())

input_data(dict(entries=[]), get_goal_file_path(goal_name))

Expand All @@ -201,8 +203,8 @@ def goals_analysis():
total_goals_next_week = 0
total_goals_next_month = 0

if os.path.isfile(GOALS_CONFIG_FILE_PATH):
with open(GOALS_CONFIG_FILE_PATH) as goals_file:
if os.path.isfile(get_goals_config_file_path()):
with open(get_goals_config_file_path()) as goals_file:
contents = yaml.load(goals_file)
for entry in contents['entries']:
total_goals += 1
Expand Down Expand Up @@ -241,9 +243,9 @@ def list_goals():
"""
get goals listed chronologically by deadlines
"""
if os.path.isfile(GOALS_CONFIG_FILE_PATH):
if os.path.isfile(get_goals_config_file_path()):

with open(GOALS_CONFIG_FILE_PATH) as goals_file:
with open(get_goals_config_file_path()) as goals_file:
contents = yaml.load(goals_file)

if len(contents):
Expand Down Expand Up @@ -305,7 +307,7 @@ def view_related_tasks():

not_valid_name = True

if os.path.isfile(GOALS_CONFIG_FILE_PATH):
if os.path.isfile(get_goals_config_file_path()):
while not_valid_name:
click.echo(chalk.blue(
'Enter the goal name that you would like to examine'))
Expand Down
Loading

0 comments on commit 626f768

Please sign in to comment.