From 16ba5048ecaf622512db4dd611fe8726005e63e0 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Sat, 10 Mar 2018 11:18:17 +0000 Subject: [PATCH] Initial hacking to start handling explicit file/contents Change-Id: I359dd280ce9edef56789c086cc377238238700e4 --- fixtures_git/__init__.py | 39 ++++++++++++++++++++-------------- tests/advanced/__init__.py | 0 tests/advanced/test_fixture.py | 34 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 tests/advanced/__init__.py create mode 100644 tests/advanced/test_fixture.py diff --git a/fixtures_git/__init__.py b/fixtures_git/__init__.py index e13f7b0..8a1e85c 100644 --- a/fixtures_git/__init__.py +++ b/fixtures_git/__init__.py @@ -299,29 +299,36 @@ class GitFixture(fixtures.Fixture): if self._graph: self.gittree = GitTree(self, self._graph, self._branches) - def _create_file(self): - contents = "\n\n".join(loremipsum.get_paragraphs(3)) + def _create_file(self, filename=None, contents=None): + if not contents: + contents = "\n\n".join(loremipsum.get_paragraphs(3)) # always want to ensure the files added to the repo are unique no # matter which branch they are added to, as otherwise there may # be conflicts caused by replaying local changes and performing # merges - while True: - tmpfile = tempfile.NamedTemporaryFile( - dir=self.repo.working_dir, delete=False) - if tmpfile.name not in self._file_list: - self._file_list.add(tmpfile.name) - break - # case where same filename in use on a different branch - tmpfile.close() - os.remove(tmpfile.name) + if filename is None: + while True: + tmpfile = tempfile.NamedTemporaryFile( + dir=self.repo.working_dir, delete=False) + # close immediately to ensure code at the end of this can + # be reopened for writing + tmpfile.close() + if tmpfile.name not in self._file_list: + self._file_list.add(tmpfile.name) + filename = tmpfile.name + break + # remove file as name in use elsewhere in the git repo + os.remove(tmpfile.name) - tmpfile.write(contents.encode('utf-8')) - tmpfile.close() - return tmpfile.name + with open(filename, 'w') as f: + f.write(contents.encode('utf-8')) - def _create_file_commit(self, change_id=None, message_prefix=None): - filename = self._create_file() + return filename + + def _create_file_commit(self, message=None, contents=None, + change_id=None, message_prefix=None): + filename = self._create_file(contents) self.repo.git.add(filename) message = "Adding %s" % os.path.basename(filename) if message_prefix: diff --git a/tests/advanced/__init__.py b/tests/advanced/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/advanced/test_fixture.py b/tests/advanced/test_fixture.py new file mode 100644 index 0000000..139e25e --- /dev/null +++ b/tests/advanced/test_fixture.py @@ -0,0 +1,34 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import testtools + +import fixtures_git + + +class TestBuildTree(testtools.TestCase): + + def test_basic(self): + pass + + def test_merge(self): + pass + + def test_merge_and_replace(self): + pass + + def test_merge_unrelated_history(self): + pass