Starting to implement POGO for Gem::Specification and co

This commit is contained in:
R. Tyler Croy 2015-08-08 16:13:55 -07:00
parent e2f245881a
commit 8b1d453484
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
7 changed files with 181 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
build/
.gradle/
*.sw*
.idea/
*.iml

View File

@ -11,6 +11,12 @@ repositories {
dependencies {
compile "org.codehaus.groovy:groovy-all:2.4.0+"
compile 'joda-time:joda-time:2.6+'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.5.4'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.4'
codenarc "org.codenarc:CodeNarc:0.24"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}

View File

@ -0,0 +1,16 @@
package com.github.jrubygradle.groovygem
import com.fasterxml.jackson.annotation.JsonProperty
import groovy.transform.TypeChecked
import sun.org.mozilla.javascript.commonjs.module.Require
/**
*/
@TypeChecked
class Dependency {
@JsonProperty
String name
@JsonProperty
List<Requirement> requirements
}

View File

@ -1,9 +1,75 @@
package com.github.jrubygradle.groovygem
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import groovy.transform.TypeChecked
/**
* Plain Old Groovy Object wrapping the data for a Gem
* Plain Old Groovy Object for an enumeration of metadata provided by a gem
*/
@TypeChecked
class Gem {
@JsonProperty
String name
String version
@JsonProperty
Version version
@JsonProperty
String description
@JsonProperty
String platform
@JsonProperty
String email
@JsonProperty
String homepage
@JsonProperty
List<String> authors = []
@JsonProperty
List<String> files
@JsonProperty(value='test_files')
List<String> testFiles
@JsonProperty
List<String> executables
@JsonProperty(value='require_paths')
List<String> requirePaths
@JsonProperty
List<String> licenses
@JsonProperty(value='specification_version')
Integer specificationVersion
@JsonProperty(value='rubygems_version')
String rubygemsVersion
static Gem fromFile(Object metadata) {
File metadataFile
ObjectMapper mapper
if (metadata instanceof String) {
metadataFile = new File(metadata)
}
else if (metadata instanceof File) {
metadataFile = metadata as File
}
if (!(metadataFile?.exists())) {
return null
}
mapper = new ObjectMapper(new YAMLFactory())
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
return mapper.readValue(metadataFile, Gem.class)
}
}

View File

@ -0,0 +1,10 @@
package com.github.jrubygradle.groovygem
import com.fasterxml.jackson.annotation.JsonProperty
import groovy.transform.TypeChecked
/**
*/
@TypeChecked
class Requirement {
}

View File

@ -0,0 +1,10 @@
package com.github.jrubygradle.groovygem
import com.fasterxml.jackson.annotation.JsonProperty
/**
*/
class Version {
@JsonProperty
String version
}

View File

@ -1,6 +1,75 @@
package com.github.jrubygradle.groovygem
import com.fasterxml.jackson.databind.DeserializationConfig
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import spock.lang.*
class GemSpec extends Specification {
final String FIXTURES_ROOT = new File(['src', 'test', 'resources'].join(File.separator)).absolutePath
final String METADATA_FIXTURE = [FIXTURES_ROOT, 'thor-0.19.1', 'metadata'].join(File.separator)
def "fromFile with an invalid/empty file should return null"() {
given:
Gem gem
when:
gem = Gem.fromFile(f)
then:
!(gem instanceof Gem)
where:
f | _
null | _
'' | _
new File('/tmp/spock') | _
}
def "fromFile with a valid file path should return a Gem"() {
given:
final String path = METADATA_FIXTURE
expect:
Gem.fromFile(path) instanceof Gem
}
def "fromFile with a valid File object should return a Gem"() {
given:
final File f = new File(METADATA_FIXTURE)
expect:
Gem.fromFile(f) instanceof Gem
}
def "a metadata file should parse into a Gem"() {
given:
Gem gem
when: "a metadata file is deserialized"
gem = Gem.fromFile(METADATA_FIXTURE)
then: "it should create an instance"
gem instanceof Gem
and: "its version should be correct"
gem.version.version == '0.19.1'
and: "its properties should equal what's in the YAML"
gem.getProperty(property) == value
where:
property | value
'name' | 'thor'
'platform' | 'ruby'
'email' | 'ruby-thor@googlegroups.com'
'authors' | ['Yehuda Katz', 'José Valim']
'executables' | ['thor']
'licenses' | ['MIT']
'homepage' | 'http://whatisthor.com/'
'rubygemsVersion' | '2.2.2'
'specificationVersion' | 4
'requirePaths' | ['lib']
}
}