I am trying to run some pytests with pants 2.0.2. ...
# general
I am trying to run some pytests with pants 2.0.2. These specific tests run a subprocess command in a function that interacts with
. I am getting the following error:
Copy code
fatal: not a git repository (or any of the parent directories): .git
I have found that this is because with pants 2 the root where the tests run is
Copy code
which does not include my “.git” config files. With pants v1 all of these tests ran without issue and im assuming the root was the “build root”. I have tried adding a global
that includes the
files necessary but that is not working. Is this as simple as changing the
of the global pants options? I would like to not have things running “inside” of my build root as it could cause other issues. Any other suggestions?
Well, not too satisfying - ie: no Pants magic, but one route is to re-write the tests to be hermetic and set up their own git repo. For example: https://github.com/pantsbuild/pants/blob/main/src/python/pants/vcs/git_test.py
Alternatively, having test
files checked in as resources should work, although there may be some futzing with the paths.
A thrird option is to plumb GIT_DIR and GIT_WORK_TREE env vars to point back at the build root. I'm not sure if this can be done portably with pants. Perhaps via pants.toml which supports "$(buildroot)s" interpolation.
Concretely, this would be the pants.toml edit in the pants repo itself:
Copy code
$ git diff
diff --git a/pants.toml b/pants.toml
index 985d8734e..05febd0aa 100644
--- a/pants.toml
+++ b/pants.toml
@@ -163,6 +163,8 @@ extra_env_vars = [
   # We'd always like complete backtraces in tests.
+  "GIT_DIR=%(buildroot)s/.git",
+  "GIT_WORK_TREE=%(buildroot)s",
Resources won't work because pants_ignore option ignores .git, which is necessary to keep I believe
John's recommendation sounds good. Only issue I see is that it will cache when it may not be safe - pants won't be able to track when the .git folder should change I think we'd need to add a field to python_tests to mark the tests as uncacheable
The env var value is part of the cache key, so things should just work.
But the env var doesn't change when .git folder changes, right?
Aha, yes - Eric is right if the tests depend on the state of the git repo.
If they just use it to branch from a known old commit, do some work, then clean up, you should be good.
👍 1
🔥 1
all my tests do not depend on state so this will work! thanks guys as always
❤️ 1
I will let you know how it works out
🤞 1
@hundreds-father-404 This wouldn't be the real
but a test one that is checked into the repo under some resources dir
Pants aside, developer to developer, I'd strongly (re) consider just having tests set up their own hermetic git repos unless there is some really pressing reason this won't work.
It sounds mildly terrifying to have tests operating on your project git repo.
oh trust me…. i didnt write these and i do not like these tests…. something i inherited. we do not setup any repos… we use git for diffs in sub proceseses in some of our clis.