test: reuse code
diff --git a/src/compatTest/groovy/org/fidata/gradle/KeygenPluginSpecification.groovy b/src/compatTest/groovy/org/fidata/gradle/KeygenPluginSpecification.groovy
index 14bedc6..3e83be9 100644
--- a/src/compatTest/groovy/org/fidata/gradle/KeygenPluginSpecification.groovy
+++ b/src/compatTest/groovy/org/fidata/gradle/KeygenPluginSpecification.groovy
@@ -20,6 +20,7 @@
package org.fidata.gradle
import spock.lang.Specification
+import spock.lang.Shared
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.BuildResult
import com.jcraft.jsch.JSch
@@ -31,6 +32,10 @@
* Specification for {@link org.fidata.gradle.KeygenPlugin} class
*/
class KeygenPluginSpecification extends Specification {
+ @Shared
+ @SuppressWarnings('PropertyName')
+ static final JSch jSch = new JSch()
+
// fields
boolean success = false
@@ -73,31 +78,31 @@
// feature methods
void 'generates ssh key with project-wide settings'() {
given: 'build file'
- buildFile << '''\
+ File privateKeyFile = new File(buildDir, 'ssh_key')
+ File publicKeyFile = new File(buildDir, 'ssh_key.pub')
+ String email = 'test@example.com'
+ buildFile << """\
keygen {
keyType = RSA
keySize = 2048
}
task('generateSSHKey', type: GenerateSSHKeyTask) {
- privateKeyFile = new File(buildDir, 'ssh_key')
- email = 'test@example.com'
+ privateKeyFile = new File(${ privateKeyFile.toString().inspect() })
+ email = ${ email.inspect() }
}
- '''.stripIndent()
+ """.stripIndent()
when: 'generateSSHKey task is run'
build('generateSSHKey')
then: 'private key file is generated'
- File privateKeyFile = new File(buildDir, 'ssh_key')
privateKeyFile.exists()
and: 'public key file is generated'
- File publicKeyFile = new File(buildDir, 'ssh_key.pub')
publicKeyFile.exists()
when: 'generated key is loaded'
- JSch jSch = new JSch()
KeyPair kpair = KeyPair.load(jSch, privateKeyFile.bytes, publicKeyFile.bytes)
then: 'no exception is thrown'
@@ -110,7 +115,7 @@
((KeyPairRSA)kpair).keySize == 2048
and: 'public key comment is set'
- kpair.publicKeyComment == 'test@example.com'
+ kpair.publicKeyComment == email
(success = true) != null
}
@@ -118,27 +123,26 @@
// feature methods
void 'generates ssh key with per-task settings'() {
given: 'build file'
- buildFile << '''\
+ File privateKeyFile = new File(buildDir, 'ssh_key')
+ File publicKeyFile = new File(buildDir, 'ssh_key.pub')
+ buildFile << """\
keygen {
keyType = RSA
keySize = 4096
}
task('generateSSHKey', type: GenerateSSHKeyTask) {
- privateKeyFile = new File(buildDir, 'ssh_key')
+ privateKeyFile = new File(${ privateKeyFile.toString().inspect() })
keyType = DSA
keySize = 2048
email = 'test@example.com'
}
- '''.stripIndent()
+ """.stripIndent()
when: 'generateSSHKey task is run'
build('generateSSHKey')
then: 'key type equals to requested'
- File privateKeyFile = new File(buildDir, 'ssh_key')
- File publicKeyFile = new File(buildDir, 'ssh_key.pub')
- JSch jSch = new JSch()
KeyPair kpair = KeyPair.load(jSch, privateKeyFile.bytes, publicKeyFile.bytes)
kpair.keyType == KeyPair.DSA
@@ -151,19 +155,20 @@
// feature methods
void 'don\'t override existing key files'() {
given: 'build file'
- buildFile << '''\
+ String dummyKey = 'Dummy key'
+ File privateKeyFile = new File(buildDir, 'ssh_key')
+ File publicKeyFile = new File(buildDir, 'ssh_key.pub')
+ buildFile << """\
task('generateSSHKey', type: GenerateSSHKeyTask) {
- privateKeyFile = new File(buildDir, 'ssh_key')
+ privateKeyFile = new File(${ privateKeyFile.toString().inspect() })
email = 'test@example.com'
}
- '''.stripIndent()
+ """.stripIndent()
and: 'private key file exists'
buildDir.mkdir()
- String dummyKey = 'Dummy key'
- File privateKeyFile = new File(buildDir, 'ssh_key')
+
privateKeyFile.text = dummyKey
- File publicKeyFile = new File(buildDir, 'ssh_key.pub')
publicKeyFile.text = dummyKey
when: 'generateSSHKey task is run'