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

# -*- 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 

 

from os import path 

from unittest import mock 

 

from snapcraft.plugins import nodejs 

from snapcraft import tests 

 

 

class NodePluginTestCase(tests.TestCase): 

 

    def setUp(self): 

        super().setUp() 

 

        patcher = mock.patch('snapcraft.common.run') 

        self.run_mock = patcher.start() 

        self.addCleanup(patcher.stop) 

 

        patcher = mock.patch('snapcraft.sources.Tar') 

        self.tar_mock = patcher.start() 

        self.addCleanup(patcher.stop) 

 

        patcher = mock.patch('sys.stdout') 

        patcher.start() 

        self.addCleanup(patcher.stop) 

 

    def test_pull_local_sources(self): 

        class Options: 

            source = '.' 

            node_packages = [] 

 

        plugin = nodejs.NodePlugin('test-part', Options()) 

 

        os.makedirs(plugin.sourcedir) 

 

        plugin.pull() 

 

        self.assertFalse(self.run_mock.called, 'run() was called') 

        self.tar_mock.assert_has_calls([ 

            mock.call( 

                nodejs._get_nodejs_release(), 

                path.join(os.path.abspath('.'), 'parts', 'test-part', 'npm')), 

            mock.call().pull()]) 

 

    def test_build_local_sources(self): 

        class Options: 

            source = '.' 

            node_packages = [] 

 

        plugin = nodejs.NodePlugin('test-part', Options()) 

 

        os.makedirs(plugin.sourcedir) 

        open(os.path.join(plugin.sourcedir, 'package.json'), 'w').close() 

 

        plugin.build() 

 

        self.run_mock.assert_has_calls([ 

            mock.call(['npm', 'install', '-g'], cwd=plugin.builddir)]) 

        self.tar_mock.assert_has_calls([ 

            mock.call( 

                nodejs._get_nodejs_release(), 

                path.join(os.path.abspath('.'), 'parts', 'test-part', 'npm')), 

            mock.call().provision(plugin.installdir)]) 

 

    def test_pull_and_build_node_packages_sources(self): 

        class Options: 

            source = None 

            node_packages = ['my-pkg'] 

 

        plugin = nodejs.NodePlugin('test-part', Options()) 

 

        os.makedirs(plugin.sourcedir) 

 

        plugin.pull() 

        plugin.build() 

 

        self.run_mock.assert_has_calls([ 

            mock.call(['npm', 'install', '-g', 'my-pkg'], 

                      cwd=plugin.builddir)]) 

        self.tar_mock.assert_has_calls([ 

            mock.call( 

                nodejs._get_nodejs_release(), 

                path.join(os.path.abspath('.'), 'parts', 'test-part', 'npm')), 

            mock.call().pull(), 

            mock.call().provision(plugin.installdir)]) 

 

    @mock.patch('platform.machine') 

    def test_unsupported_arch_raises_exception(self, machine_mock): 

        machine_mock.return_value = 'fantasy-arch' 

 

        class Options: 

            source = None 

            node_packages = [] 

 

        with self.assertRaises(EnvironmentError) as raised: 

            nodejs.NodePlugin('test-part', Options()) 

 

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

                         'architecture not supported (fantasy-arch)') 

 

    def test_schema(self): 

        self.assertEqual( 

            nodejs.NodePlugin.schema(), 

            {'$schema': 'http://json-schema.org/draft-04/schema#', 

             'properties': { 

                 'node-packages': {'default': [], 

                                   'items': {'type': 'string'}, 

                                   'minitems': 1, 

                                   'type': 'array', 

                                   'uniqueItems': True}, 

                 'source': {'type': 'string'}, 

                 'source-branch': {'default': '', 'type': 'string'}, 

                 'source-subdir': {'default': None, 'type': 'string'}, 

                 'source-tag': {'default': '', 'type:': 'string'}, 

                 'source-type': {'default': '', 'type': 'string'}}, 

             'type': 'object'}) 

 

    @mock.patch('snapcraft.BasePlugin.schema') 

    def test_required_not_in_parent_schema(self, schema_mock): 

        schema_mock.return_value = {'properties': {}} 

 

        self.assertTrue('required' not in nodejs.NodePlugin.schema())