Example #1
0
def test_content_large_diff_logged(output, root):
    path = "path"
    p = Content(path, content="\n".join(["asdf"] * 21))
    p._max_diff = 20
    p._max_diff_lead = 5
    root.component += p
    with open(p.path, "w") as f:
        f.write("\n".join(["bsdf"] * 21))
    root.component.deploy()
    log = os.listdir(p.diff_dir)[0]
    with open(os.path.join(p.diff_dir, log)) as f:
        assert (f.read() == """\
---
+++
@@ -1,21 +1,21 @@
""" + "\n".join(["-bsdf"] * 21) + "\n" + "\n".join(["+asdf"] * 21) + "\n")

    assert output.backend.output == Ellipsis("""\
host > MyComponent > Content('work/mycomponent/path')
More than 20 lines of diff. Showing first and last 5 lines.
see ... for the full diff.
  path ---
  path +++
  path @@ -1,21 +1,21 @@
  path -bsdf
  path -bsdf
  path ...
  path +asdf
  path +asdf
  path +asdf
  path +asdf
  path +asdf
""")
Example #2
0
def test_content_passed_by_string(root):
    path = "path"
    p = Content(path, content="asdf")
    root.component += p
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == "asdf"
Example #3
0
def test_content_passed_by_string_template(root):
    path = 'path'
    root.component.foobar = 'asdf'
    p = Content(path, content='{{component.foobar}}')
    root.component += p
    with open(p.path, 'w') as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == 'asdf'
Example #4
0
def test_content_passed_by_string_notemplate(root):
    path = "path"
    root.component.foobar = "asdf"
    p = Content(path, content="{{component.foobar}}", is_template=False)
    root.component += p
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == "{{component.foobar}}"
Example #5
0
def test_content_relative_source_path_computed_wrt_definition_dir(root):
    path = "path"
    source = "source"
    with open(source, "w") as f:
        f.write("asdf")
    p = Content(path, source=source)
    root.component += p
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == "asdf"
Example #6
0
def test_content_passed_by_file_using_path_as_default(root):
    path = "path"
    with open(path, "w") as f:
        f.write("content from source file")
    p = Content(path)
    root.component += p
    with open(p.path, "w") as f:
        # Ensure file exists
        pass
    assert p.source == root.defdir + "/path"
    root.component.deploy()
    with open(p.path) as f:
        # Actually, as source and target are the same: nothing happened.
        assert f.read() == "content from source file"
Example #7
0
def test_content_passed_by_file_defaults_to_utf8(root):
    source = "source"
    with open(source, "w", encoding="utf-8") as f:
        f.write("cöntent from source file")
    path = "path"
    p = Content(path, source=source)
    root.component += p
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path, encoding="utf-8") as f:
        assert f.read() == "cöntent from source file"
Example #8
0
def test_content_passed_by_file_handles_encoding_on_verify(root):
    source = "source"
    path = "path"
    p = Content(path, source=source, encoding="latin-1")
    root.component += p
    with open(source, "w", encoding="latin-1") as f:
        f.write("cöntent from source file")
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path, encoding="latin-1") as f:
        assert f.read() == "cöntent from source file"
Example #9
0
def test_content_passed_by_file_handles_encoding_on_verify(root):
    source = 'source'
    path = 'path'
    p = Content(path, source=source, encoding='latin-1')
    root.component += p
    with open(source, 'w', encoding='latin-1') as f:
        f.write('cöntent from source file')
    with open(p.path, 'w') as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path, encoding='latin-1') as f:
        assert f.read() == 'cöntent from source file'
Example #10
0
def test_content_passed_by_file_defaults_to_utf8(root):
    source = 'source'
    with open(source, 'w', encoding='utf-8') as f:
        f.write('cöntent from source file')
    path = 'path'
    p = Content(path, source=source)
    root.component += p
    with open(p.path, 'w') as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path, encoding='utf-8') as f:
        assert f.read() == 'cöntent from source file'
Example #11
0
def test_content_passed_by_file_template(root):
    source = "source"
    with open(source, "w") as f:
        f.write("{{component.foobar}}")
    path = "path"
    root.component.foobar = "asdf"
    p = Content(path, source=source, is_template=True)
    root.component += p
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == "asdf"
Example #12
0
def test_content_passed_by_file_template(root):
    source = 'source'
    with open(source, 'w') as f:
        f.write('{{component.foobar}}')
    path = 'path'
    root.component.foobar = 'asdf'
    p = Content(path, source=source, is_template=True)
    root.component += p
    with open(p.path, 'w') as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == 'asdf'
Example #13
0
def test_content_template_with_explicit_context(root):
    path = 'path'
    context = Mock()
    context.foobar = 'asdf'
    p = Content(path,
                content='{{component.foobar}}',
                is_template=True,
                template_context=context)
    root.component += p
    with open(p.path, 'w') as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path) as f:
        assert f.read() == 'asdf'
Example #14
0
def test_content_passed_by_file_no_template_is_binary(root):
    # This is a regression test for #14944 where UTF 8 in a
    # non-template file caused an accidental implicit encoding to ASCII
    source = "source"
    with open(source, "wb") as f:
        f.write(b"\x89PNG\r\n\x1a\n")
    path = "path"
    p = Content(path, source=source, is_template=False, encoding=None)
    root.component.foo = "foo"
    root.component += p
    with open(p.path, "w") as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path, "rb") as f:
        assert f.read() == (b"\x89PNG\r\n\x1a\n")
Example #15
0
def test_content_passed_by_file_no_template_is_binary(root):
    # This is a regression test for #14944 where UTF 8 in a
    # non-template file caused an accidental implicit encoding to ASCII
    source = 'source'
    with open(source, 'wb') as f:
        f.write('cöntent \x00from source file {{component.foo}}'.
                encode('utf-8'))
    path = 'path'
    p = Content(path, source=source, is_template=False)
    root.component.foo = 'foo'
    root.component += p
    with open(p.path, 'w') as f:
        # The content component assumes there's a file already in place. So
        # we need to create it.
        pass
    root.component.deploy()
    with open(p.path, 'rb') as f:
        assert f.read() == (
            b'c\xc3\xb6ntent \x00from source file {{component.foo}}')
Example #16
0
def test_content_only_required_changes_touch_file(root):
    path = "path"
    p = Content(path, content="asdf")
    root.component += p
    # Lets start with an existing file that has the wrong content:
    with open(p.path, "w") as f:
        f.write("bsdf")
    os.utime(p.path, (0, 0))
    # Modify mtime so we can prove it's been changed.
    root.component.deploy()
    stat = os.stat(p.path)
    assert stat.st_mtime != 0

    os.utime(p.path, (0, 0))
    stat = os.stat(p.path)
    assert stat.st_mtime == 0

    root.component.deploy()
    stat = os.stat(p.path)
    assert stat.st_mtime == 0
Example #17
0
    def configure(self):
        self._eval_attr('dist_sources', 'sources', 'vcs_update',
                        'hg_hostfingerprints')

        self.hg_hostfingerprints = collections.OrderedDict(
            x for x in sorted(self.hg_hostfingerprints.items()))

        self.provide('source', self)

        additional_hgrc = os.path.join(self.defdir, 'hgrc')
        if not self.additional_hgrc_content and os.path.exists(
                additional_hgrc):
            self |= Content('additional_hgrc', source=additional_hgrc)
            self.additional_hgrc_content = self._.content

        self.hgrc = File('~/.hgrc', source=pkg_resources.resource_filename(
            'batou_scm', 'resources/hgrc'))
        self += self.hgrc

        self.distributions = dict(
            self.add_clone(url) for url in self.dist_sources)
        self.clones = self.distributions.copy()
        self.clones.update(self.add_clone(url) for url in self.sources)
Example #18
0
def test_content_does_not_allow_both_content_and_source(root):
    path = "path"
    with pytest.raises(ValueError):
        root.component += Content(path, content="asdf", source="bsdf")
Example #19
0
def test_content_does_not_allow_both_content_and_source(root):
    path = 'path'
    with pytest.raises(ValueError):
        root.component += Content(path, content='asdf', source='bsdf')