e3554080356e9baa429fe224acf2b14099193d43
[sdc/sdc-distribution-client.git] /
1 # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Copyright (C) 2013 Association of Universities for Research in Astronomy
17 #                    (AURA)
18 #
19 # Redistribution and use in source and binary forms, with or without
20 # modification, are permitted provided that the following conditions are met:
21 #
22 #     1. Redistributions of source code must retain the above copyright
23 #        notice, this list of conditions and the following disclaimer.
24 #
25 #     2. Redistributions in binary form must reproduce the above
26 #        copyright notice, this list of conditions and the following
27 #        disclaimer in the documentation and/or other materials provided
28 #        with the distribution.
29 #
30 #     3. The name of AURA and its representatives may not be used to
31 #        endorse or promote products derived from this software without
32 #        specific prior written permission.
33 #
34 # THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED
35 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 # DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,
38 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40
41 import os
42 import textwrap
43
44 from testtools.content import text_content
45 from testtools.matchers import Contains, EndsWith
46
47 from pbr.tests import base
48 from pbr.tests import util
49
50
51 class TestHooks(base.BaseTestCase):
52     def setUp(self):
53         super(TestHooks, self).setUp()
54         with util.open_config(
55                 os.path.join(self.package_dir, 'setup.cfg')) as cfg:
56             cfg.set('global', 'setup-hooks',
57                     'pbr_testpackage._setup_hooks.test_hook_1\n'
58                     'pbr_testpackage._setup_hooks.test_hook_2')
59             cfg.set('build_ext', 'pre-hook.test_pre_hook',
60                     'pbr_testpackage._setup_hooks.test_pre_hook')
61             cfg.set('build_ext', 'post-hook.test_post_hook',
62                     'pbr_testpackage._setup_hooks.test_post_hook')
63
64     def test_global_setup_hooks(self):
65         """Test setup_hooks.
66
67         Test that setup_hooks listed in the [global] section of setup.cfg are
68         executed in order.
69         """
70
71         stdout, _, return_code = self.run_setup('egg_info')
72         assert 'test_hook_1\ntest_hook_2' in stdout
73         assert return_code == 0
74
75     def test_command_hooks(self):
76         """Test command hooks.
77
78         Simple test that the appropriate command hooks run at the
79         beginning/end of the appropriate command.
80         """
81
82         stdout, _, return_code = self.run_setup('egg_info')
83         assert 'build_ext pre-hook' not in stdout
84         assert 'build_ext post-hook' not in stdout
85         assert return_code == 0
86
87         stdout, stderr, return_code = self.run_setup('build_ext')
88         self.addDetailUniqueName('stderr', text_content(stderr))
89         assert textwrap.dedent("""
90             running build_ext
91             running pre_hook pbr_testpackage._setup_hooks.test_pre_hook for command build_ext
92             build_ext pre-hook
93         """) in stdout  # flake8: noqa
94         self.expectThat(stdout, EndsWith('build_ext post-hook'))
95         assert return_code == 0
96
97     def test_custom_commands_known(self):
98         stdout, _, return_code = self.run_setup('--help-commands')
99         self.assertFalse(return_code)
100         self.assertThat(stdout, Contains(" testr "))