Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for copying. Setup project for pytest. #88

Merged
merged 12 commits into from
Oct 18, 2018

Conversation

pyasi
Copy link
Contributor

@pyasi pyasi commented Oct 18, 2018

  • Added tests for critical paths in the copying functionality
  • Made a small refactor to the INVALID dirs to be a constant. Thus they can be referenced anywhere and changed in one place
  • Used pytest as requested in Testing Suite #69

This is just a start to the testing journey possibilities! But wanted to get my feet wet in the repository.

Copy link
Owner

@alichtman alichtman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start on the tests! Check out the comments I left and let me know if you have any questions.

constants.py Outdated
@@ -7,3 +7,5 @@ class Constants:
URL='https://github.com/alichtman/shallow-backup'
AUTHOR_EMAIL='aaronlichtman@gmail.com'
CONFIG_PATH='.shallow-backup'
INVALIDS = [".Trash", ".npm", ".cache", ".rvm"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the name a bit more descriptive, but this is a good refactor.

Suggested change
INVALIDS = [".Trash", ".npm", ".cache", ".rvm"]
INVALID_DIRS = [".Trash", ".npm", ".cache", ".rvm"]

@@ -121,7 +121,7 @@ def copy_dir(source_dir, backup_path):
"""
Copy dotfolder from $HOME.
"""
invalid = {".Trash", ".npm", ".cache", ".rvm"}
invalid = set(Constants.INVALIDS)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
invalid = set(Constants.INVALIDS)
invalid = set(Constants.INVALID_DIRS)

from shallow_backup import _copy_file, copy_dir
from constants import Constants

TEST_DIR = 'test-directory'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename these all to be shallow-backup-... to decrease the chance of conflicting file/directory names and accidentally destroying someone's data.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And shorten directory to just dir

Test that copying a directory works as expected
"""
dir_to_copy = '/test/'
os.mkdir(TEST_DIR + dir_to_copy)
Copy link
Owner

@alichtman alichtman Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better practice to use os.path.join() for creating paths since that's cross-platform compatible. Let's do that here.

for directory in DIRS:
try:
os.mkdir(directory)
except FileExistsError:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the directories exist already, instead of removing them immediately (since the teardown methods should remove them, so if they exist, it's possible the directories weren't created by us), we should print an error message and ask the user whether they want to remove the files or exit, using the inquirer library in the same style as the CLI.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'm open to talk about this one. With the file name change to be much less generic, the prompt may be overkill and could interfere with scripting. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I did this so that if it was ever run in a CI environment we shouldn't expect to have any issues. A prompt in a CI environment would hang the build.

We can try to be smarter about this and append the current datetime to definitely avoid conflicts and then remove all dirs with a suffix shallow-copy-dir-*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I think that might be a bit of over-engineering for a simple unit test solution. Going to keep as is for now but I'm happy to throw it is (simple change) if you deem necessary

Copy link
Owner

@alichtman alichtman Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try to be smarter about this and append the current datetime to definitely avoid conflicts and then remove all dirs with a suffix shallow-copy-dir-*

This is a great solution -- let's do it.

Copy link
Owner

@alichtman alichtman Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further consideration, let's do shallow-backup-test-dir with no date after it because it's unlikely that someone has a pre-existing dir named that which isn't used by shallow-backup.

Pipfile Outdated
@@ -8,6 +8,7 @@ click = "*"
inquirer = "*"
gitpython = "*"
colorama = "*"
pytest = "*"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a Pipfile.lock update, as well? Can you commit that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took care of the Pipfile changes in my previous PR, so this is on master already. You should remove the Pipfile and Pipfile.lock files from your commit (if they differ from the ones I committed. If they're the same, git will just take care of it.)

@pyasi
Copy link
Contributor Author

pyasi commented Oct 18, 2018

@alichtman I addressed all comments besides the ones I explained. Let me know if you still have concerns on those choices.

@alichtman alichtman closed this Oct 18, 2018
@alichtman alichtman reopened this Oct 18, 2018
@alichtman
Copy link
Owner

alichtman commented Oct 18, 2018

Two minor changes and we can get this merged in, @pyasi

from shallow_backup import _copy_file, _copy_dir
from constants import Constants

TEST_DIR = 'shallow-backup-test-dir'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name one of these TEST_DIR_FOR_BACKUP and one of them TEST_DIR_TO_BACKUP so there's no confusion about which is which.

pyasi and others added 2 commits October 18, 2018 18:09
@alichtman alichtman merged commit 8516612 into alichtman:master Oct 18, 2018
@alichtman
Copy link
Owner

alichtman commented Oct 18, 2018

In reading through these tests again, there are some definite improvements that could be made. I've added TODOs for these in the code, but I wanted to get the structure merged in so other people to work on it, as well. I've opened a bunch of other testing issues, if you're interested in taking a look at those. I think it's really important to have a comprehensive testing suite for this, and I'll work on building it out over time, but the help right now is definitely appreciated.

Again, thanks for the contribution. I'd be excited to work on some more features with you in the future.

@pyasi
Copy link
Contributor Author

pyasi commented Oct 18, 2018

One of your test changes broke a test. Let's get in the habit of running the suite before commiting/merging. I'll take a look at some of the other testable pieces.

@alichtman
Copy link
Owner

alichtman commented Oct 18, 2018

Let's get in the habit of running the suite before commiting/merging.

You're totally correct. I did a quick refactor in the GitHub editor without actually pulling it to test. That was sloppy...

Is the error on line 42? test_dir = 'test/' was changed to test_dir = 'test'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants