~nskaggs/juju-release-tools/generate-release-notes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Tests for apply_patches script used in tarball building."""

import contextlib
import mock
import os
import StringIO
import sys
import unittest

from apply_patches import (
    apply_patch,
    get_arg_parser,
    main,
)
from utils import (
    temp_dir,
)


class TestArgParser(unittest.TestCase):

    def parse_args(self, args):
        parser = get_arg_parser()
        return parser.parse_args(args)

    def test_defaults(self):
        args = self.parse_args(["patches/", "."])
        self.assertEqual("patches/", args.patchdir)
        self.assertEqual(".", args.srctree)
        self.assertEqual(False, args.dry_run)
        self.assertEqual(False, args.verbose)

    def test_dry_run(self):
        args = self.parse_args(["--dry-run", "patches/", "."])
        self.assertEqual("patches/", args.patchdir)
        self.assertEqual(".", args.srctree)
        self.assertEqual(True, args.dry_run)
        self.assertEqual(False, args.verbose)

    def test_verbose(self):
        args = self.parse_args(["patches/", ".", "--verbose"])
        self.assertEqual("patches/", args.patchdir)
        self.assertEqual(".", args.srctree)
        self.assertEqual(False, args.dry_run)
        self.assertEqual(True, args.verbose)


class TestApplyPatches(unittest.TestCase):

    def assert_open_only(self, open_mock, filename):
        self.assertEqual(
            open_mock.mock_calls,
            [
                mock.call(filename),
                mock.call().__enter__(),
                mock.call().__exit__(None, None, None),
            ]
        )

    def test_with_defaults(self):
        open_mock = mock.mock_open()
        with mock.patch("subprocess.call", autospec=True) as call_mock:
            with mock.patch("apply_patches.open", open_mock, create=True):
                apply_patch("some.patch", "a/tree")
        self.assert_open_only(open_mock, "some.patch")
        call_mock.assert_called_once_with(
            ["patch", "-f", "-u", "-p1", "-r-"],
            stdin=open_mock(),
            cwd="a/tree",
            )

    def test_dry_run_verbose(self):
        open_mock = mock.mock_open()
        with mock.patch("subprocess.call", autospec=True) as call_mock:
            with mock.patch("apply_patches.open", open_mock, create=True):
                apply_patch("trial.diff", "a/tree", True, True)
        self.assert_open_only(open_mock, "trial.diff")
        call_mock.assert_called_once_with(
            ["patch", "-f", "-u", "-p1", "-r-", "--dry-run", "--verbose"],
            stdin=open_mock(),
            cwd="a/tree",
            )

    def test_verbose(self):
        open_mock = mock.mock_open()
        with mock.patch("subprocess.call", autospec=True) as call_mock:
            with mock.patch("apply_patches.open", open_mock, create=True):
                apply_patch("scary.patch", "a/tree", verbose=True)
        self.assert_open_only(open_mock, "scary.patch")
        call_mock.assert_called_once_with(
            ["patch", "-f", "-u", "-p1", "-r-", "--verbose"],
            stdin=open_mock(),
            cwd="a/tree",
            )


class TestMain(unittest.TestCase):

    @contextlib.contextmanager
    def patch_output(self):
        messages = []

        def fake_print(message, file=None):
            self.assertEqual(file, sys.stderr)
            messages.append(message)

        with mock.patch("apply_patches.print", fake_print, create=True):
            yield messages

    def test_no_patchdir(self):
        stream = StringIO.StringIO()
        with temp_dir() as basedir:
            with self.assertRaises(SystemExit):
                with mock.patch("sys.stderr", stream):
                    main(["test", os.path.join(basedir, "missing"), basedir])
        self.assertRegexpMatches(
            stream.getvalue(), "Could not list patch directory: .*/missing")

    def test_no_srctree(self):
        stream = StringIO.StringIO()
        with temp_dir() as basedir:
            with self.assertRaises(SystemExit):
                with mock.patch("sys.stderr", stream):
                    main(["test", basedir, os.path.join(basedir, "missing")])
        self.assertRegexpMatches(
            stream.getvalue(), "Source tree '.*/missing' not a directory")

    def test_one_patch(self):
        with temp_dir() as basedir:
            patchdir = os.path.join(basedir, "patches")
            os.mkdir(patchdir)
            patchfile = os.path.join(patchdir, "sample.diff")
            open(patchfile, "w").close()
            with self.patch_output() as messages:
                with mock.patch(
                        "apply_patches.apply_patch", return_value=0,
                        autospec=True) as ap_mock:
                    main(["test", patchdir, basedir])
            ap_mock.assert_called_once_with(patchfile, basedir, False, False)
            self.assertEqual(messages, [
                u"Applying 1 patch", u"Applied patch 'sample.diff'"
            ])

    def test_bad_patches(self):
        with temp_dir() as basedir:
            patchdir = os.path.join(basedir, "patches")
            os.mkdir(patchdir)
            patch_a = os.path.join(patchdir, "a.patch")
            open(patch_a, "w").close()
            patch_b = os.path.join(patchdir, "b.patch")
            open(patch_b, "w").close()
            with self.patch_output() as messages:
                with mock.patch(
                        "apply_patches.apply_patch", return_value=1,
                        autospec=True) as ap_mock:
                    main(["test", "--verbose", patchdir, basedir])
            ap_mock.assert_called_once_with(patch_a, basedir, False, True)
            self.assertEqual(messages, [
                u"Applying 2 patches", u"Failed to apply patch 'a.patch'"
            ])

    def test_non_patch(self):
        with temp_dir() as basedir:
            patchdir = os.path.join(basedir, "patches")
            os.mkdir(patchdir)
            patch_a = os.path.join(patchdir, "readme.txt")
            open(patch_a, "w").close()
            with self.patch_output() as messages:
                with mock.patch(
                        "apply_patches.apply_patch", autospec=True) as ap_mock:
                    main(["test", "--verbose", patchdir, basedir])
            self.assertEqual(ap_mock.mock_calls, [])
            self.assertEqual(messages, [u"Applying 0 patches"])