|
| 1 | +package com.ableton |
| 2 | + |
| 3 | +import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString |
| 4 | +import static org.junit.Assert.assertEquals |
| 5 | +import static org.junit.Assert.assertNotNull |
| 6 | +import static org.junit.Assert.assertTrue |
| 7 | + |
| 8 | +import com.lesfurets.jenkins.unit.BasePipelineTest |
| 9 | +import org.junit.After |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | + |
| 13 | + |
| 14 | +class PipenvTest extends BasePipelineTest { |
| 15 | + @SuppressWarnings('FieldTypeRequired') |
| 16 | + def script |
| 17 | + |
| 18 | + @Override |
| 19 | + @Before |
| 20 | + void setUp() throws Exception { |
| 21 | + super.setUp() |
| 22 | + |
| 23 | + this.script = loadScript('test/resources/EmptyPipeline.groovy') |
| 24 | + assertNotNull(script) |
| 25 | + helper.registerAllowedMethod('sh', [Map], JenkinsMocks.sh) |
| 26 | + helper.registerAllowedMethod('sh', [String], JenkinsMocks.sh) |
| 27 | + |
| 28 | + JenkinsMocks.addShMock('pipenv --rm', '', 0) |
| 29 | + } |
| 30 | + |
| 31 | + @After |
| 32 | + void tearDown() { |
| 33 | + JenkinsMocks.clearStaticData() |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void runCommand() throws Exception { |
| 38 | + List pythonVersions = ['2.7', '3.5'] |
| 39 | + pythonVersions.each { python -> |
| 40 | + JenkinsMocks.addShMock("pipenv install --dev --python ${python}", '', 0) |
| 41 | + } |
| 42 | + JenkinsMocks.addShMock('pipenv run ./test.py', '', 0) |
| 43 | + |
| 44 | + new Pipenv(script: script).runCommand( |
| 45 | + command: './test.py', |
| 46 | + pythonVersions: pythonVersions, |
| 47 | + ) |
| 48 | + |
| 49 | + pythonVersions.each { python -> |
| 50 | + assertTrue(helper.callStack.findAll { call -> |
| 51 | + call.methodName == 'sh' |
| 52 | + }.any { call -> |
| 53 | + callArgsToString(call).contains("pipenv install --dev --python ${python}") |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + // We must use a stupid variable name here ('call2') in order to get around CodeNarc's |
| 58 | + // slightly buggy CouldBeSwitchStatement rule. |
| 59 | + List pipenvRunCalls = helper.callStack.findAll { call2 -> |
| 60 | + if (call2.methodName == 'sh') { |
| 61 | + return callArgsToString(call2).contains('pipenv run ./test.py') |
| 62 | + } |
| 63 | + } |
| 64 | + assertEquals(pythonVersions.size(), pipenvRunCalls.size()) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void runCommandReturnStdout() throws Exception { |
| 69 | + List pythonVersions = ['2.7', '3.5'] |
| 70 | + pythonVersions.each { python -> |
| 71 | + JenkinsMocks.addShMock("pipenv install --dev --python ${python}", '', 0) |
| 72 | + } |
| 73 | + JenkinsMocks.addShMock('pipenv run ./test.py', 'success', 0) |
| 74 | + |
| 75 | + Map result = new Pipenv(script: script).runCommand( |
| 76 | + command: './test.py', |
| 77 | + pythonVersions: pythonVersions, |
| 78 | + returnStdout: true, |
| 79 | + ) |
| 80 | + |
| 81 | + assertEquals(2, result.size()) |
| 82 | + pythonVersions.each { python -> |
| 83 | + assertEquals('success', result[python]) |
| 84 | + } |
| 85 | + |
| 86 | + pythonVersions.each { python -> |
| 87 | + assertTrue(helper.callStack.findAll { call -> |
| 88 | + call.methodName == 'sh' |
| 89 | + }.any { call -> |
| 90 | + callArgsToString(call).contains("pipenv install --dev --python ${python}") |
| 91 | + }) |
| 92 | + } |
| 93 | + List pipenvRunCalls = helper.callStack.findAll { call -> |
| 94 | + if (call.methodName == 'sh') { |
| 95 | + return callArgsToString(call).contains('pipenv run ./test.py') |
| 96 | + } |
| 97 | + } |
| 98 | + assertEquals(pythonVersions.size(), pipenvRunCalls.size()) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void runCommandReturnStatus() throws Exception { |
| 103 | + List pythonVersions = ['2.7', '3.5'] |
| 104 | + pythonVersions.each { python -> |
| 105 | + JenkinsMocks.addShMock("pipenv install --dev --python ${python}", '', 0) |
| 106 | + } |
| 107 | + JenkinsMocks.addShMock('pipenv run ./test.py', '', 3) |
| 108 | + |
| 109 | + Map result = new Pipenv(script: script).runCommand( |
| 110 | + command: './test.py', |
| 111 | + pythonVersions: pythonVersions, |
| 112 | + returnStatus: true, |
| 113 | + ) |
| 114 | + |
| 115 | + assertEquals(2, result.size()) |
| 116 | + pythonVersions.each { python -> |
| 117 | + assertEquals(3, result[python]) |
| 118 | + } |
| 119 | + |
| 120 | + // Ensure that pipenv install was called for each Python version |
| 121 | + pythonVersions.each { python -> |
| 122 | + assertTrue(helper.callStack.findAll { call -> |
| 123 | + call.methodName == 'sh' |
| 124 | + }.any { call -> |
| 125 | + callArgsToString(call).contains("pipenv install --dev --python ${python}") |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + // Ensure that pipenv run was called for each Python version |
| 130 | + List pipenvRunCalls = helper.callStack.findAll { call -> |
| 131 | + if (call.methodName == 'sh') { |
| 132 | + return callArgsToString(call).contains('pipenv run ./test.py') |
| 133 | + } |
| 134 | + } |
| 135 | + assertEquals(pythonVersions.size(), pipenvRunCalls.size()) |
| 136 | + |
| 137 | + // Ensure that pipenv --rm was called |
| 138 | + assertTrue(helper.callStack.findAll { call -> |
| 139 | + call.methodName == 'sh' |
| 140 | + }.any { call -> |
| 141 | + callArgsToString(call).contains('pipenv --rm') |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + @Test(expected = AssertionError) |
| 147 | + void runCommandNoScript() throws Exception { |
| 148 | + new Pipenv().runCommand(script: 'foo', pythonVersions: ['2.7']) |
| 149 | + } |
| 150 | + |
| 151 | + @Test(expected = AssertionError) |
| 152 | + void runCommandNoCommand() throws Exception { |
| 153 | + new Pipenv(script: script).runCommand(pythonVersions: ['2.7']) |
| 154 | + } |
| 155 | + |
| 156 | + @Test(expected = AssertionError) |
| 157 | + void runCommandNoPythonVersions() throws Exception { |
| 158 | + new Pipenv(script: script).runCommand(command: 'foo') |
| 159 | + } |
| 160 | + |
| 161 | + @Test(expected = AssertionError) |
| 162 | + void runCommandEmptyPythonVersions() throws Exception { |
| 163 | + new Pipenv(script: script).runCommand(command: 'foo', pythonVersions: []) |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void runWith() throws Exception { |
| 168 | + List pythonVersions = ['2.7', '3.5'] |
| 169 | + pythonVersions.each { python -> |
| 170 | + JenkinsMocks.addShMock("pipenv install --dev --python ${python}", '', 0) |
| 171 | + } |
| 172 | + |
| 173 | + int numCalls = 0 |
| 174 | + Map result = new Pipenv(script: script).runWith(pythonVersions: pythonVersions) { p -> |
| 175 | + numCalls++ |
| 176 | + return p |
| 177 | + } |
| 178 | + |
| 179 | + // Ensure that pipenv install was called for each Python version |
| 180 | + pythonVersions.each { python -> |
| 181 | + assertTrue(helper.callStack.findAll { call -> |
| 182 | + call.methodName == 'sh' |
| 183 | + }.any { call -> |
| 184 | + callArgsToString(call).contains("pipenv install --dev --python ${python}") |
| 185 | + }) |
| 186 | + } |
| 187 | + |
| 188 | + // Ensure that the closure body was evaluated for each Python version |
| 189 | + assertEquals(pythonVersions.size(), numCalls) |
| 190 | + pythonVersions.each { python -> |
| 191 | + assert result[python] |
| 192 | + assertEquals(python, result[python]) |
| 193 | + } |
| 194 | + |
| 195 | + // Ensure that pipenv --rm was called |
| 196 | + assertTrue(helper.callStack.findAll { call -> |
| 197 | + call.methodName == 'sh' |
| 198 | + }.any { call -> |
| 199 | + callArgsToString(call).contains('pipenv --rm') |
| 200 | + }) |
| 201 | + } |
| 202 | + |
| 203 | + @Test(expected = AssertionError) |
| 204 | + void runWithNoScript() throws Exception { |
| 205 | + new Pipenv().runWith(pythonVersions: ['2.7']) {} |
| 206 | + } |
| 207 | + |
| 208 | + @Test(expected = AssertionError) |
| 209 | + void runWithEmptyPythonVersions() throws Exception { |
| 210 | + new Pipenv(script: script).runWith(pythonVersions: []) {} |
| 211 | + } |
| 212 | +} |
0 commit comments