Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- 

# 

# Copyright (C) 2015 Canonical Ltd 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License version 3 as 

# published by the Free Software Foundation. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

 

import os 

import tempfile 

import unittest.mock 

 

import snapcraft 

from snapcraft import sources 

from snapcraft import tests 

 

 

class MockOptions: 

 

    def __init__(self, source, source_type=None, source_branch=None, 

                 source_tag=None): 

        self.source = source 

        self.source_type = source_type 

        self.source_branch = source_branch 

        self.source_tag = source_tag 

 

 

class TestBasePlugin(tests.TestCase): 

 

    def test_get_source_with_unrecognized_source_must_raise_exception(self): 

        options = MockOptions('unrecognized://test_source') 

        plugin = snapcraft.BasePlugin('test_plugin', options) 

        with self.assertRaises(ValueError) as raised: 

            plugin.pull() 

 

        self.assertEqual(raised.exception.__str__(), 

                         'no handler to manage source') 

 

    @unittest.mock.patch('os.path.isdir') 

    def test_local_non_dir_source_path_must_raise_exception(self, mock_isdir): 

        options = MockOptions('file') 

        mock_isdir.return_value = False 

        plugin = snapcraft.BasePlugin('test_plugin', options) 

        with self.assertRaises(ValueError) as raised: 

            plugin.pull() 

 

        mock_isdir.assert_called_once_with('file') 

 

        self.assertEqual(raised.exception.__str__(), 

                         'local source is not a directory') 

 

    def test_build_with_subdir_copies_subdir(self): 

        class Options: 

            source_subdir = 'src' 

 

        plugin = snapcraft.BasePlugin('test-part', Options()) 

 

        tmpdir = tempfile.TemporaryDirectory() 

        self.addCleanup(tmpdir.cleanup) 

        plugin.sourcedir = tmpdir.name 

        subdir = os.path.join(plugin.sourcedir, plugin.options.source_subdir) 

        os.mkdir(subdir) 

        open(os.path.join(subdir, 'file'), 'w').close() 

 

        tmpdir = tempfile.TemporaryDirectory() 

        self.addCleanup(tmpdir.cleanup) 

        plugin.builddir = tmpdir.name 

 

        plugin.build() 

 

        self.assertTrue(os.path.exists(os.path.join(plugin.builddir, 'file'))) 

 

    def test_build_without_subdir_copies_sourcedir(self): 

        class Options: 

            pass 

 

        plugin = snapcraft.BasePlugin('test-part', Options()) 

 

        tmpdir = tempfile.TemporaryDirectory() 

        self.addCleanup(tmpdir.cleanup) 

        plugin.sourcedir = tmpdir.name 

        subdir = os.path.join(plugin.sourcedir, 'src') 

        os.mkdir(subdir) 

        open(os.path.join(subdir, 'file'), 'w').close() 

 

        tmpdir = tempfile.TemporaryDirectory() 

        self.addCleanup(tmpdir.cleanup) 

        plugin.builddir = tmpdir.name 

 

        plugin.build() 

 

        self.assertTrue(os.path.exists( 

            os.path.join(plugin.builddir, 'src', 'file'))) 

 

 

class GetSourceWithBranches(tests.TestCase): 

 

    scenarios = [ 

        ('git with source branch and tag', { 

            'source_type': 'git', 

            'source_branch': 'test_branch', 

            'source_tag': 'tag', 

        }), 

        ('hg with source branch and tag', { 

            'source_type': 'mercurial', 

            'source_branch': 'test_branch', 

            'source_tag': 'tag', 

        }), 

    ] 

 

    def test_get_source_with_branch_and_tag_must_raise_error(self): 

        options = MockOptions('lp:source', self.source_type, 

                              self.source_branch, self.source_tag) 

        plugin = snapcraft.BasePlugin('test_plugin', options) 

        with self.assertRaises(sources.IncompatibleOptionsError) as raised: 

            plugin.pull() 

 

        self.assertEqual( 

            raised.exception.__str__(), 

            'can\'t specify both source-tag and source-branch for a {} ' 

            'source'.format(self.source_type)) 

 

 

class GetSourceTestCase(tests.TestCase): 

 

    scenarios = [ 

        ('bzr with source branch', { 

            'source_type': 'bzr', 

            'source_branch': 'test_branch', 

            'source_tag': None, 

            'error': 'source-branch'}), 

        ('tar with source branch', { 

            'source_type': 'tar', 

            'source_branch': 'test_branch', 

            'source_tag': None, 

            'error': 'source-branch'}), 

        ('tar with source tag', { 

            'source_type': 'tar', 

            'source_branch': None, 

            'source_tag': 'test_tag', 

            'error': 'source-tag'}) 

    ] 

 

    def test_get_source_with_branch_must_raise_error(self): 

        options = MockOptions('lp:this', self.source_type, self.source_branch, 

                              self.source_tag) 

        plugin = snapcraft.BasePlugin('test_plugin', options) 

 

        with self.assertRaises(sources.IncompatibleOptionsError) as raised: 

            plugin.pull() 

 

        self.assertEqual( 

            raised.exception.__str__(), 

            'can\'t specify a {} for a {} source'.format( 

                self.error, self.source_type)) 

 

 

class BuildTestCase(tests.TestCase): 

 

    def test_do_not_follow_links(self): 

        options = MockOptions('.', None, None, None) 

        plugin = snapcraft.BasePlugin('test_plugin', options) 

 

        os.makedirs(plugin.partdir) 

        os.symlink(os.path.abspath('.'), plugin.sourcedir) 

 

        # Create a file and a symlink to it 

        open('file', mode='w').close() 

        os.symlink('file', 'symlinkfile') 

 

        # Create a directory and a symlink to it 

        os.mkdir('dir') 

        os.symlink('dir', 'symlinkdir') 

        plugin.build() 

 

        # Make sure this is still a link 

        self.assertTrue(os.path.islink(plugin.sourcedir)) 

 

        build_file_path = os.path.join( 

            plugin.builddir, 'file') 

        build_symlinkfile_path = os.path.join( 

            plugin.builddir, 'symlinkfile') 

 

        self.assertTrue(os.path.isfile(build_file_path)) 

        self.assertTrue(os.path.islink(build_symlinkfile_path)) 

 

        build_dir_path = os.path.join( 

            plugin.builddir, 'dir') 

        build_symlinkdir_path = os.path.join( 

            plugin.builddir, 'symlinkdir') 

 

        self.assertTrue(os.path.isdir(build_dir_path)) 

        self.assertTrue(os.path.islink(build_symlinkdir_path))