fix: treeNotSorted issue · Pull Request #1799 · gitpython-developers/GitPython

EliahKagan added a commit to EliahKagan/GitPython that referenced this pull request

@EliahKagan

This fixes a test bug in TestTree.test_tree_modifier_ordering
where:

- A `tmp` subdirectory of the directory the tests are run from
  (almost always the root of the GitPython source tree) was
  created, instead of creating it in /tmp or elsewhere configured.

- That directory was never deleted afterward, creating new
  untracked files in GitPython's own working tree, and also making
  it so running the tests twice would always fail the second time,
  unless a manual intervening deletion was performed. (This had not
  broken CI, since CI checks clone the repository anew each time.)

- The directory was changed into to set up the test, but not
  deterministically changed back out of. It would typically be
  exited, but this was not guaranteed to occur if some subprocess
  commands were to fail unexpectedly.

In addition to fixing all three aspects of that bug, this also:

- Remains in the temporary directory only as long as necessary to
  execute the subprocesses in it that produce the expected value
  for the test (including not entering it until running them).

- Deletes the temporary directory immediately after exiting it,
  since it is only used to produce a file list to compare to.

- Avoids interleaving file creation and git subprocess commands,
  since doing so made it harder to understand what was being set up
  and since the difference is not relevant to the test.

- Does the work in that temporary directory before performing any
  operations with the code under test, because it is conceptually
  an "arrange" step, and because doing so makes its limited purpose
  clearer on reading the tests.

- Some other refactoring to support the fixes and accompanying
  changes, including extracting the temporary directory logic to a
  helper method.

This deliberately does not change the order in which any files are
created. It also keeps the approach of using subprocess functions
directly to operate on the temporary git repository (and changing
directory while doing so, keeping each of the commands the same),
since there might be good reasons to do this, such as to make very
clear that the file order being obtained to compare to is really
coming from git itself. Even if this is to be changed, it seems
outside the scope of this bugfix.

The test still fails if the fix to git/objects/tree.py from 365d44f
(gitpython-developers#1799) is absent. This was verified by running:

    git revert --no-commit 365d44f
    pytest --no-cov -vv test/test_tree.py
    git revert --abort