Пример #1
0
 def test_appends_custom_section_initially(self):
     original = factory.make_name('Original-text')
     custom_text = factory.make_name('Custom-text')
     header, footer = maas_custom_config_markers
     self.assertEqual(
         [original, header, custom_text, footer],
         write_custom_config_section(original, custom_text).splitlines())
Пример #2
0
 def test_appends_custom_section_initially(self):
     original = factory.make_name('Original-text')
     custom_text = factory.make_name('Custom-text')
     header, footer = maas_custom_config_markers
     self.assertEqual(
         [original, header, custom_text, footer],
         write_custom_config_section(original, custom_text).splitlines())
Пример #3
0
def run(args):
    """Customize a config file.

    Reads a custom configuration section from standard input, and the given
    configuration file.  Prints to standard output a copy of the file with
    the custom section appended, or substituted for an existing custom
    section if there already was one.
    """
    with open(args.file, 'rb') as original_file:
        original_text = original_file.read().decode(args.encoding)
    custom_section = sys.stdin.read().decode(args.encoding)
    new_text = write_custom_config_section(original_text, custom_section)
    sys.stdout.write(new_text.encode(args.encoding))
Пример #4
0
def run(args):
    """Customize a config file.

    Reads a custom configuration section from standard input, and the given
    configuration file.  Prints to standard output a copy of the file with
    the custom section appended, or substituted for an existing custom
    section if there already was one.
    """
    with open(args.file, 'rb') as original_file:
        original_text = original_file.read().decode(args.encoding)
    custom_section = sys.stdin.read().decode(args.encoding)
    new_text = write_custom_config_section(original_text, custom_section)
    sys.stdout.write(new_text.encode(args.encoding))
Пример #5
0
 def test_ignores_footer_before_header(self):
     # Custom-section footers before the custom-section header are
     # ignored.  You might see this if there was an older custom
     # config section whose header has been changed or deleted.
     header, footer = maas_custom_config_markers
     original = [
         footer,
         "Possible old custom section.",
         ]
     expected = [
         footer,
         "Possible old custom section.",
         header,
         "New custom section.",
         footer,
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #6
0
 def test_replaces_custom_section_only(self):
     header, footer = maas_custom_config_markers
     original = [
         "Text before custom section.",
         header,
         "Old custom section.",
         footer,
         "Text after custom section.",
         ]
     expected = [
         "Text before custom section.",
         header,
         "New custom section.",
         footer,
         "Text after custom section.",
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #7
0
 def test_ignores_footer_before_header(self):
     # Custom-section footers before the custom-section header are
     # ignored.  You might see this if there was an older custom
     # config section whose header has been changed or deleted.
     header, footer = maas_custom_config_markers
     original = [
         footer,
         "Possible old custom section.",
         ]
     expected = [
         footer,
         "Possible old custom section.",
         header,
         "New custom section.",
         footer,
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #8
0
 def test_replaces_custom_section_only(self):
     header, footer = maas_custom_config_markers
     original = [
         "Text before custom section.",
         header,
         "Old custom section.",
         footer,
         "Text after custom section.",
         ]
     expected = [
         "Text before custom section.",
         header,
         "New custom section.",
         footer,
         "Text after custom section.",
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #9
0
 def test_ignores_header_without_footer(self):
     # If the footer of the custom config section is not found,
     # write_custom_config_section will pretend that the header is not
     # there and append a new custom section.  This does mean that there
     # will be two headers and one footer; a subsequent rewrite will
     # replace everything from the first header to the footer.
     header, footer = maas_custom_config_markers
     original = [
         header,
         "Old custom section (probably).",
         ]
     expected = [
         header,
         "Old custom section (probably).",
         header,
         "New custom section.",
         footer,
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #10
0
 def test_ignores_second_header(self):
     # If there are two custom-config headers but only one footer,
     # write_custom_config_section will treat everything between the
     # first header and the footer as custom config section, which it
     # will overwrite.
     header, footer = maas_custom_config_markers
     original = [
         header,
         "Old custom section (probably).",
         header,
         "More custom section.",
         footer,
         ]
     expected = [
         header,
         "New custom section.",
         footer,
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #11
0
 def test_ignores_header_without_footer(self):
     # If the footer of the custom config section is not found,
     # write_custom_config_section will pretend that the header is not
     # there and append a new custom section.  This does mean that there
     # will be two headers and one footer; a subsequent rewrite will
     # replace everything from the first header to the footer.
     header, footer = maas_custom_config_markers
     original = [
         header,
         "Old custom section (probably).",
         ]
     expected = [
         header,
         "Old custom section (probably).",
         header,
         "New custom section.",
         footer,
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #12
0
 def test_ignores_second_header(self):
     # If there are two custom-config headers but only one footer,
     # write_custom_config_section will treat everything between the
     # first header and the footer as custom config section, which it
     # will overwrite.
     header, footer = maas_custom_config_markers
     original = [
         header,
         "Old custom section (probably).",
         header,
         "More custom section.",
         footer,
         ]
     expected = [
         header,
         "New custom section.",
         footer,
         ]
     self.assertEqual(
         expected,
         write_custom_config_section(
             '\n'.join(original), "New custom section.").splitlines())
Пример #13
0
    def test_produces_sensible_text(self):
        # The other tests mostly operate on lists of lines, because it
        # eliminates problems with line endings.  This test here
        # verifies that the actual text you get is sensible, preserves
        # newlines, and generally looks normal.
        header, footer = maas_custom_config_markers
        original = dedent("""\
            Top.


            More.
            %s
            Old custom section.
            %s
            End.

            """) % (header, footer)
        new_custom_section = dedent("""\
            New custom section.

            With blank lines.""")
        expected = dedent("""\
            Top.


            More.
            %s
            New custom section.

            With blank lines.
            %s
            End.

            """) % (header, footer)
        self.assertEqual(
            expected,
            write_custom_config_section(original, new_custom_section))
Пример #14
0
    def test_produces_sensible_text(self):
        # The other tests mostly operate on lists of lines, because it
        # eliminates problems with line endings.  This test here
        # verifies that the actual text you get is sensible, preserves
        # newlines, and generally looks normal.
        header, footer = maas_custom_config_markers
        original = dedent("""\
            Top.


            More.
            %s
            Old custom section.
            %s
            End.

            """) % (header, footer)
        new_custom_section = dedent("""\
            New custom section.

            With blank lines.""")
        expected = dedent("""\
            Top.


            More.
            %s
            New custom section.

            With blank lines.
            %s
            End.

            """) % (header, footer)
        self.assertEqual(
            expected,
            write_custom_config_section(original, new_custom_section))
Пример #15
0
 def test_custom_section_ends_with_newline(self):
     self.assertThat(write_custom_config_section("x", "y"), EndsWith('\n'))
Пример #16
0
 def test_preserves_indentation_in_original(self):
     indented_text = "   text."
     self.assertIn(
         indented_text,
         write_custom_config_section(indented_text, "Custom section."))
Пример #17
0
 def test_preserves_indentation_in_custom_section(self):
     indented_text = "   custom section."
     self.assertIn(
         indented_text,
         write_custom_config_section("Original.", indented_text))
Пример #18
0
 def test_preserves_indentation_in_original(self):
     indented_text = "   text."
     self.assertIn(
         indented_text,
         write_custom_config_section(indented_text, "Custom section."))
Пример #19
0
 def test_preserves_indentation_in_custom_section(self):
     indented_text = "   custom section."
     self.assertIn(
         indented_text,
         write_custom_config_section("Original.", indented_text))
Пример #20
0
 def test_custom_section_ends_with_newline(self):
     self.assertThat(write_custom_config_section("x", "y"), EndsWith('\n'))