Skip to content

Commit

Permalink
gitdist: Move to Base Dir (TriBITSPub#212)
Browse files Browse the repository at this point in the history
The ability to use the environment variable GITDIST_MOVE_TO_BASE_DIR to
change gitdist's behavior is now documented (see
--dist-help=move-to-base-dir) and tested (see the
gitdist_move_to_base_dir unit test).
  • Loading branch information
jmgate committed Nov 2, 2017
1 parent 3b2f98a commit c7d9665
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 5 deletions.
116 changes: 112 additions & 4 deletions test/python_utils/gitdist_UnitTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def __init__(self, useGit):
class test_createAsciiTable(unittest.TestCase):


def setUp(self):
self.gitdistMoveToBaseDir = os.environ["GITDIST_MOVE_TO_BASE_DIR"]
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = ""


def tearDown(self):
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = self.gitdistMoveToBaseDir


def test_full_table(self):
tableData = [
{ "label" : "ID", "align" : "R",
Expand Down Expand Up @@ -197,6 +206,15 @@ def test_row_mismatch(self):
class test_gitdist_getRepoStats(unittest.TestCase):


def setUp(self):
self.gitdistMoveToBaseDir = os.environ["GITDIST_MOVE_TO_BASE_DIR"]
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = ""


def tearDown(self):
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = self.gitdistMoveToBaseDir


def test_no_change(self):
try:
testDir = createAndMoveIntoTestDir("gitdist_getRepoStats_no_change")
Expand Down Expand Up @@ -562,7 +580,12 @@ class test_gitdist_getRepoVersionDictFromRepoVersionFileString(unittest.TestCase


def setUp(self):
None
self.gitdistMoveToBaseDir = os.environ["GITDIST_MOVE_TO_BASE_DIR"]
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = ""


def tearDown(self):
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = self.gitdistMoveToBaseDir


def test_repoVersionFile_withSummary_1(self):
Expand Down Expand Up @@ -620,13 +643,20 @@ def assertContainsAllGitdistHelpSections(testObj, cmndOut):
GeneralScriptSupport.extractLinesMatchingRegex(cmndOut,"^USAGE TIPS:$"), "USAGE TIPS:\n")
testObj.assertEqual(
GeneralScriptSupport.extractLinesMatchingRegex(cmndOut,"^SCRIPT DEPENDENCIES:$"), "SCRIPT DEPENDENCIES:\n")
testObj.assertEqual(
GeneralScriptSupport.extractLinesMatchingRegex(cmndOut,"^MOVE TO BASE DIRECTORY:$"), "MOVE TO BASE DIRECTORY:\n")


class test_gitdist(unittest.TestCase):


def setUp(self):
None
self.gitdistMoveToBaseDir = os.environ["GITDIST_MOVE_TO_BASE_DIR"]
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = ""


def tearDown(self):
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = self.gitdistMoveToBaseDir


def test_default(self):
Expand Down Expand Up @@ -676,15 +706,15 @@ def test_dist_help_all_help(self):
# Tet that --dist-help --help prints nice error message
def test_dist_help_help(self):
cmndOut = getCmndOutput(gitdistPath+" --dist-help --help")
cmndOut_expected = "gitdist: error: option --dist-help: invalid choice: '--help' (choose from '', 'overview', 'repo-selection-and-setup', 'dist-repo-status', 'repo-versions', 'aliases', 'usage-tips', 'script-dependencies', 'all')\n"
cmndOut_expected = "gitdist: error: option --dist-help: invalid choice: '--help' (choose from '', 'overview', 'repo-selection-and-setup', 'dist-repo-status', 'repo-versions', 'aliases', 'usage-tips', 'script-dependencies', 'move-to-base-dir', 'all')\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))


# Test --dist-helps=invalid-pick picked up as invalid value.
def test_dist_help_invalid_pick_help(self):
cmndOut = getCmndOutput(gitdistPath+" --dist-help=invalid-pick --help")
assertContainsGitdistHelpHeader(self, cmndOut)
errorToFind = "gitdist: error: option --dist-help: invalid choice: 'invalid-pick' (choose from '', 'overview', 'repo-selection-and-setup', 'dist-repo-status', 'repo-versions', 'aliases', 'usage-tips', 'script-dependencies', 'all')"
errorToFind = "gitdist: error: option --dist-help: invalid choice: 'invalid-pick' (choose from '', 'overview', 'repo-selection-and-setup', 'dist-repo-status', 'repo-versions', 'aliases', 'usage-tips', 'script-dependencies', 'move-to-base-dir', 'all')"
self.assertEqual(
GeneralScriptSupport.extractLinesMatchingSubstr(cmndOut,errorToFind), errorToFind+"\n")

Expand Down Expand Up @@ -1400,5 +1430,83 @@ def test_dist_repo_status_extra_args_fail(self):
os.chdir(testBaseDir)


def test_gitdist_move_to_base_dir(self):
os.chdir(testBaseDir)
try:

# Create a mock git meta-project.
testDir = createAndMoveIntoTestDir("gitdist_move_to_base_dir")
os.makedirs("ExtraRepo/path/to/somewhere")
open(".gitdist", "w").write(
".\n" \
"ExtraRepo\n"
)
open("ExtraRepo/.gitdist", "w").write(
".\n"
)
os.chdir("ExtraRepo/path/to/somewhere")

# Test with the default setting.
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = ""
cmndOut = GeneralScriptSupport.getCmndOutput(gitdistPathMock+" status")
cmndOut_expected = \
"\n*** Base Git Repo: somewhere\n" \
"['mockgit', 'status']\n\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))

# Test moving up the directory tree until we find a .gitdist file.
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = "IMMEDIATE_BASE"
cmndOut = GeneralScriptSupport.getCmndOutput(gitdistPathMock+" status")
cmndOut_expected = \
"\n*** Base Git Repo: ExtraRepo\n" \
"['mockgit', 'status']\n\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))

# Test moving up the directory tree until we find the outer-most .gitdist
# file.
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = "EXTREME_BASE"
cmndOut = GeneralScriptSupport.getCmndOutput(gitdistPathMock+" status")
cmndOut_expected = \
"\n*** Base Git Repo: MockProjectDir\n" \
"['mockgit', 'status']\n\n" \
"*** Git Repo: ExtraRepo\n" \
"['mockgit', 'status']\n\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))

# Rename the .gitdist files .gitdist.default, and try the tests again.
os.rename("../../../.gitdist", "../../../.gitdist.default")
os.rename("../../../../.gitdist", "../../../../.gitdist.default")

# Test with the default setting.
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = ""
cmndOut = GeneralScriptSupport.getCmndOutput(gitdistPathMock+" status")
cmndOut_expected = \
"\n*** Base Git Repo: somewhere\n" \
"['mockgit', 'status']\n\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))

# Test moving up the directory tree until we find a .gitdist file.
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = "IMMEDIATE_BASE"
cmndOut = GeneralScriptSupport.getCmndOutput(gitdistPathMock+" status")
cmndOut_expected = \
"\n*** Base Git Repo: ExtraRepo\n" \
"['mockgit', 'status']\n\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))

# Test moving up the directory tree until we find the outer-most .gitdist
# file.
os.environ["GITDIST_MOVE_TO_BASE_DIR"] = "EXTREME_BASE"
cmndOut = GeneralScriptSupport.getCmndOutput(gitdistPathMock+" status")
cmndOut_expected = \
"\n*** Base Git Repo: MockProjectDir\n" \
"['mockgit', 'status']\n\n" \
"*** Git Repo: ExtraRepo\n" \
"['mockgit', 'status']\n\n"
self.assertEqual(s(cmndOut), s(cmndOut_expected))

finally:
os.chdir(testBaseDir)


if __name__ == '__main__':
unittest.main()
52 changes: 51 additions & 1 deletion tribits/python_utils/gitdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def s(x):
'repo-versions',
'aliases',
'usage-tips',
'script-dependencies'
'script-dependencies',
'move-to-base-dir'
]


Expand Down Expand Up @@ -613,6 +614,55 @@ def getHelpTopicsStr():
helpTopicsDict.update( { 'script-dependencies' : scriptDependenciesHelp } )


moveToBaseDirHelp = r"""
MOVE TO BASE DIRECTORY:
By default, when you run gitdist, it will look in your current working
directory for a .gitdist[.default] file. If it fails to find one, it will
treat the current directory as the base git repository (as if there was a
.gitdist file in it, having a single line with only "." in it) and then run as
usual. You have the ability to change this behavior by setting the
GITDIST_MOVE_TO_BASE_DIR environment variable. Valid settings include:
IMMEDIATE_BASE:
In this case, gitdist will start moving up the directory tree until it finds
a .gitdist[.default] file, and then run in the directory where it finds it.
For instance, say you have the following directory tree:
BaseRepo/
.git
.gitdist
...
ExtraRepo/
.git
.gitdist
...
path/
...
to/
...
some/
...
directory/
...
If you are in BaseRepo/ExtraRepo/path/to/some/directory when you run gitdist,
it will move up to ExtraRepo to execute the command you give it.
EXTREME_BASE:
In this case, gitdist will continue moving up the directory tree until it
finds the outer-most repository containing a .gitdist[.default] file, and
then run in that directory. Given the directory tree above, if you were in
BaseRepo/ExtraRepo/path/to/some/directory, it will move up to BaseRepo to
execute the command you give it.
With either of the settings above, when gitdist is finished running, it will
leave you in the same directory you were in when you executed command in the
first place. Additionally, if no .gitdist[.default] file can be found, gitdist
will execute the command you give it in your current working directory, as if
GITDIST_MOVE_TO_BASE_DIR hadn't been set.
"""
helpTopicsDict.update( { 'move-to-base-dir' : moveToBaseDirHelp } )


#
# Functions to help Format an ASCII table
#
Expand Down

0 comments on commit c7d9665

Please sign in to comment.