def test_url_patch(mock_patch_stage, filename, sha256, archive_sha256, config): # Make a patch object url = 'file://' + filename s = Spec('patch').concretized() patch = spack.patch.UrlPatch(s.package, url, sha256=sha256, archive_sha256=archive_sha256) # make a stage with Stage(url) as stage: # TODO: url isn't used; maybe refactor Stage stage.mirror_path = mock_patch_stage mkdirp(stage.source_path) with working_dir(stage.source_path): # write a file to be patched with open('foo.txt', 'w') as f: f.write("""\ first line second line """) # write the expected result of patching. with open('foo-expected.txt', 'w') as f: f.write("""\ zeroth line first line third line """) # apply the patch and compare files patch.fetch() patch.apply(stage) patch.clean() with working_dir(stage.source_path): assert filecmp.cmp('foo.txt', 'foo-expected.txt')
def test_url_patch(mock_patch_stage, filename, sha256, archive_sha256): # Make a patch object url = 'file://' + filename pkg = spack.repo.get('patch') patch = spack.patch.UrlPatch(pkg, url, sha256=sha256, archive_sha256=archive_sha256) # make a stage with Stage(url) as stage: # TODO: url isn't used; maybe refactor Stage stage.mirror_path = mock_patch_stage mkdirp(stage.source_path) with working_dir(stage.source_path): write_file("foo.txt", file_to_patch) write_file("foo-expected.txt", expected_patch_result) # apply the patch and compare files patch.fetch() patch.apply(stage) patch.clean() with working_dir(stage.source_path): assert filecmp.cmp('foo.txt', 'foo-expected.txt')
def test_patch_no_file(): # Give it the attributes we need to construct the error message FakePackage = collections.namedtuple('FakePackage', ['name', 'namespace', 'fullname']) fp = FakePackage('fake-package', 'test', 'fake-package') with pytest.raises(ValueError, match='FilePatch:'): spack.patch.FilePatch(fp, 'nonexistent_file', 0, '') patch = spack.patch.Patch(fp, 'nonexistent_file', 0, '') patch.path = 'test' with pytest.raises(spack.patch.NoSuchPatchError, match='No such patch:'): patch.apply('')
def test_url_patch(mock_stage, filename, sha256, archive_sha256): # Make a patch object url = 'file://' + filename pkg = spack.repo.get('patch') patch = spack.patch.UrlPatch(pkg, url, sha256=sha256, archive_sha256=archive_sha256) # make a stage with Stage(url) as stage: # TODO: url isn't used; maybe refactor Stage # TODO: there is probably a better way to mock this. stage.mirror_path = mock_stage # don't disrupt the spack install # Fake a source path and ensure the directory exists with working_dir(stage.path): mkdirp(spack.stage._source_path_subdir) with working_dir(stage.source_path): # write a file to be patched with open('foo.txt', 'w') as f: f.write("""\ first line second line """) # write the expected result of patching. with open('foo-expected.txt', 'w') as f: f.write("""\ zeroth line first line third line """) # apply the patch and compare files patch.fetch(stage) patch.apply(stage) patch.clean() with working_dir(stage.source_path): assert filecmp.cmp('foo.txt', 'foo-expected.txt')
def test_apply_patch_twice(mock_patch_stage, tmpdir): """Ensure that patch doesn't fail if applied twice.""" stage = DIYStage(str(tmpdir)) with tmpdir.as_cwd(): write_file("foo.txt", file_to_patch) write_file("foo-expected.txt", expected_patch_result) write_file("foo.patch", patch_file) FakePackage = collections.namedtuple('FakePackage', ['name', 'namespace', 'fullname']) fake_pkg = FakePackage('fake-package', 'test', 'fake-package') def make_patch(filename): path = os.path.realpath(str(tmpdir.join(filename))) url = 'file://' + path sha256 = spack.util.crypto.checksum("sha256", path) return spack.patch.UrlPatch(fake_pkg, url, sha256=sha256) # apply the first time patch = make_patch('foo.patch') patch.fetch() patch.apply(stage) with working_dir(stage.source_path): assert filecmp.cmp('foo.txt', 'foo-expected.txt') # ensure apply() is idempotent patch.apply(stage) with working_dir(stage.source_path): assert filecmp.cmp('foo.txt', 'foo-expected.txt') # now write a file that can't be patched with working_dir(stage.source_path): write_file("foo.txt", file_patch_cant_apply_to) # this application should fail with a real error with pytest.raises(spack.util.executable.ProcessError): patch.apply(stage)