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

Refactor code for finding home directory on Windows #322

Merged
merged 7 commits into from
Feb 23, 2018

Conversation

jdblischak
Copy link
Contributor

The main change in this PR is refactoring ssh_path to create a new function home that returns the user's home directory regardless of operating system. It is currently used by ssh_path and config.

xref: #317 #320

It also has two other minor changes:

  1. The empty .gitconfig is only created if the user has specified configuration options
  2. I fixed a typo in my previous unit test. I passed email to config instead of user.email. This generated a warning message on my local Windows machine. Should that have been detected on AppVeyor? I used the conditional statement used by testthat::skip_on_appveyor, but I don't know if that works outside of testthat (in other words, is the environment variable "APPVEYOR" created by AppVeyor or testthat?).

Signed-off-by: John Blischak <jdblischak@gmail.com>
Signed-off-by: John Blischak <jdblischak@gmail.com>
@coveralls
Copy link

coveralls commented Feb 20, 2018

Coverage Status

Coverage increased (+0.01%) to 78.531% when pulling fa9872f on jdblischak:windows-home into 95115f9 on ropensci:master.

@jdblischak
Copy link
Contributor Author

The good news is that AppVeyor is running my tests. The bad news is that the tests are failing!

It failed on the initial check that .gitconfig does not exist after just checking the configuration. This suggests that perhaps there is already a .gitconfig file present on AppVeyor. Based on the appveyor.yml in this repo and the appveyor helper script from r-appveyor, I don't expect there to be a .gitconfig present.

I'm not sure how to debug this. The tests pass for me on my local Windows machine:

export APPVEYOR=True
make check

Any ideas?

@stewid
Copy link
Member

stewid commented Feb 20, 2018

I look into it tomorrow

R/credential.R Outdated
}

# Return the user's home directory regardless of operating system
home <- function() {
Copy link
Member

Choose a reason for hiding this comment

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

I'd consider calling this git_home() for added clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it's not specific to Git because it's currently used to find the home directory for both .gitconfig and .ssh/. What about calling it something like user_home() or home_user()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stewid Your opinion?

Copy link
Member

Choose a reason for hiding this comment

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

... code for finding home directory ...

What about home_dir?

@jennybc
Copy link
Member

jennybc commented Feb 20, 2018

Seems like this should be exploiting the new 'git_config_files() function to detect existing config, for internal consistency.

Signed-off-by: John Blischak <jdblischak@gmail.com>
@jdblischak
Copy link
Contributor Author

Seems like this should be exploiting the new 'git_config_files() function to detect existing config, for internal consistency.

@jennybc Good idea! I updated the code to check for the existence of global config file using git_config_files().

@stewid
Copy link
Member

stewid commented Feb 21, 2018

I added artifacts to appveyor.yml to debug and found that "C:\Users\appveyor/.gitconfig" already exists.

> ## Check location of .gitconfig on Windows
> if (identical(Sys.getenv("APPVEYOR"), "True")) {
+   ## TEST
+   str(Sys.getenv("USERPROFILE"))
+   str(Sys.getenv("HOMEDRIVE"))
+   gitconfig_expected <- file.path(Sys.getenv("USERPROFILE"), ".gitconfig")
+   str(gitconfig_expected)
+   str(file.exists(gitconfig_expected))
+ 
+   config(global = TRUE, user.name = "name", email = "email")
+   gitconfig_expected <- file.path(Sys.getenv("HOMEDRIVE"), "Users",
+                                   Sys.info()["login"])
+   stopifnot(file.exists(gitconfig_expected))
+   unlink(gitconfig_expected)
+ }
 chr "C:\\Users\\appveyor"
 chr "C:"
 chr "C:\\Users\\appveyor/.gitconfig"
 logi TRUE

Maybe it works to:

  1. Move "C:\Users\appveyor/.gitconfig" to a temp-config-file
  2. Run tests
  3. Move temp-config-file "C:\Users\appveyor/.gitconfig"

@jdblischak
Copy link
Contributor Author

I added artifacts to appveyor.yml to debug and found that "C:\Users\appveyor/.gitconfig" already exists.

OK. That explains the problem then. Thanks!

Move "C:\Users\appveyor/.gitconfig" to a temp-config-file
Run tests
Move temp-config-file "C:\Users\appveyor/.gitconfig"

Yes, I'll temporarily move that file for these tests (and only on AppVeyor).

Had to unify AppVeyor tests of config() in tests/config.R.

Signed-off-by: John Blischak <jdblischak@gmail.com>
Signed-off-by: John Blischak <jdblischak@gmail.com>
tests/config.R Outdated
str(normalizePath("~"))
str(git_config_files())

## Temporarily move AppVeyor .gitconfig
Copy link
Member

Choose a reason for hiding this comment

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

Why do you want to move .gitconfig? Would it make more sense to write a config test that operates on local git config for a temporary repo? That would seem to require less special case-y ness.

If you're going to do such things, withr is a very handy package for doing some work in a temporarily modified state, with automatic cleanup/restoration. I don't know how much this comes up in other tests -- this would probably be a testing shift beyond the scope of this one test or PR. But worth thinking about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do you want to move .gitconfig? Would it make more sense to write a config test that operates on local git config for a temporary repo? That would seem to require less special case-y ness.

Because this unit test is to test if config() creates the global .gitconfig in the correct location on Windows.

If you're going to do such things, withr is a very handy package for doing some work in a temporarily modified state, with automatic cleanup/restoration. I don't know how much this comes up in other tests -- this would probably be a testing shift beyond the scope of this one test or PR. But worth thinking about.

withr looks super useful. I am going to have to start incorporating it into the unit tests for my own packages. However, since this isn't my repo, I am trying to follow the style of the existing tests. I'm happy to use withr if you'd prefer I do.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, withr looks very useful. However, for this single case I prefer to keep it as it is and not add a dependency.

@jdblischak
Copy link
Contributor Author

I found the bug that is causing the test to fail (at least the test is working like it should...). I'm re-building (which takes forever on Windows) and testing locally first, but will push update soon.

Signed-off-by: John Blischak <jdblischak@gmail.com>
Signed-off-by: John Blischak <jdblischak@gmail.com>
@stewid stewid merged commit 358fcd2 into ropensci:master Feb 23, 2018
@stewid
Copy link
Member

stewid commented Feb 23, 2018

Thanks

@jdblischak jdblischak deleted the windows-home branch March 13, 2018 17:32
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.

None yet

4 participants