Skip to content

Commit

Permalink
Refactored IntegTest to become unit test. (#237)
Browse files Browse the repository at this point in the history
* Refactored IntegTest to become unit test.

PackageSettingsIntegrationTest was flakey and really just wanted to
ensure that specific arguments were passed to pip. So now of running a
build, we will validate that the arguments would have been passed to
pip. This makes the tests fast and not rely on the env. It also means
that we don't need to do any strange regex's and rely on side affects of
the build processes to validate things.

The previous test was asserting that Gradle was passing args to the
commandline, and that's the same as asserting that our code was calling
Gradle with the correct parameters.

This change splits the integ test into two tests, because it was testing
two different things. `pip install` and `pip wheel`, so now there is one
test for each.

To do this, two actions were extracted. PipInstall and PipWheel. They
both use the same base class so we can re-use as much code as possible.

* Addressing Zvezdan's comments

- Deleted unused class
- Fixed some package declerations
- Using PipWheelAction in BuildWheelTask

* Updated tests to assert rebuild of wheels.

Other misc changes to codebase based on feedback

* Moving code to Java

Noticed that this was all java, so removing the Groovy plugin and the
classes are moved into the Java sourceSet

* Added more test coverage

Tests now verity skipping behavior

* Test location matches package definition

* Fixed FailureReasonProvider for BuildWheels

This change adds an integ test a few tasks that also use
FailureReasonProvider.

AbstractPythonMainSourceDefaultTask was updated to always add details to
the result call.
  • Loading branch information
ethankhall authored and zvezdan committed Jul 25, 2018
1 parent 2246e4f commit d1f50ab
Show file tree
Hide file tree
Showing 25 changed files with 1,288 additions and 842 deletions.
1 change: 0 additions & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id "java-gradle-plugin"
id 'groovy'
id 'idea'
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-all.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.linkedin.gradle.python.plugin


import com.linkedin.gradle.python.plugin.testutils.DefaultProjectLayoutRule
import com.linkedin.gradle.python.plugin.testutils.PyGradleTestBuilder
import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import spock.lang.Specification

class FailureReasonProviderIntegrationTest extends Specification {

@Rule
final DefaultProjectLayoutRule testProjectDir = new DefaultProjectLayoutRule()

@SuppressWarnings("GStringExpressionWithinString")
def 'will report errors nicely'() {
given:
testProjectDir.buildFile << """\
| plugins {
| id 'com.linkedin.python-pex'
| }
|
| configurations {
| testProj
| }
|
| dependencies {
| testProj 'pypi:pyflakes:+'
| }
|
| task testWheelTask(type: com.linkedin.gradle.python.tasks.BuildWheelsTask) {
| dependsOn 'installProject'
| packageSettings = new DefaultTestPackageSettings(project.projectDir)
| installFileCollection = configurations.testProj
| }
|
| import com.linkedin.gradle.python.util.DefaultPackageSettings
| import com.linkedin.gradle.python.util.PackageInfo
| import com.linkedin.gradle.python.tasks.execution.FailureReasonProvider
|
| class DefaultTestPackageSettings extends DefaultPackageSettings {
| DefaultTestPackageSettings(File projectDir) { super(projectDir) }
|
| @Override
| List<String> getSupportedLanguageVersions(PackageInfo packageInfo) {
| return ['2.8']
| }
| }
|
| class MyTaskListener implements TaskExecutionListener {
|
| @Override
| void beforeExecute(Task task) {
| }
|
| @Override
| void afterExecute(Task task, TaskState taskState) {
| if (task instanceof FailureReasonProvider && taskState.failure != null) {
| println(task.reason.readLines().collect { "#\${task.name}>> \$it" }.join("\\n"))
| }
| }
| }
|
| project.gradle.addListener(new MyTaskListener())
|
| version = '1.0.0'
| ${ PyGradleTestBuilder.createRepoClosure() }
""".stripMargin().stripIndent()

testProjectDir.newFile('foo/test/foo.py').text = "import os #something"

when:
def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('testWheelTask', "pytest", "flake8", '--continue')
.withPluginClasspath()
.withDebug(true)
.buildAndFail()
println result.output

def outputLines = result.output.readLines()
.findAll { it.startsWith("#") && it.contains(">>") }
.collect { it.replace(testProjectDir.root.absolutePath, '/root') }

then:
// Output structure is "#${taskName}>> ${message}
// Map is task name to lines it should have. Each line is a "contains" operation, not equals.
def expectedErrors = [
'testWheelTask': [
'Package pyflakes works only with Python versions: [2.8]'
],
'pytest' : [
"Traceback:",
"test/test_a.py:1: in <module>",
" from foo.hello import generate_welcome",
"E ImportError: No module named hello",
],
'flake8' : [
"/foo/test/foo.py:1:1: F401 'os' imported but unused",
"/foo/test/foo.py:1:10: E261 at least two spaces before inline comment",
"/foo/test/foo.py:1:11: E262 inline comment should start with '# '"
]
]
expectedErrors.each { task, lines ->
def taskLines = outputLines
.findAll { it.startsWith("#$task>> ") }
.collect { it.replace("#$task>> ", '') }
lines.each { line ->
assert taskLines.any { it.contains(line) }
}
}
}
}
Loading

0 comments on commit d1f50ab

Please sign in to comment.