Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
Fix #135 resolveSwaggerTemplate fails if multiple JARs are given
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Mar 20, 2019
1 parent ce14cc3 commit f8c7f30
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import org.gradle.api.tasks.Sync

/**
* A task to extract template files from a dependency.
*
* @author Hidetake Iwata
*/
class ResolveSwaggerTemplate extends Sync {

def ResolveSwaggerTemplate() {
ResolveSwaggerTemplate() {
from {
if (!project.configurations.swaggerTemplate.empty) {
project.zipTree(project.configurations.swaggerTemplate.singleFile)
} else {
[]
project.configurations.swaggerTemplate.resolve().collect {
project.zipTree(it)
}
}
into("${project.buildDir}/swagger-template")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.hidetake.gradle.swagger.generator
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder

import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

class Fixture {
static Project projectWithPlugin(@DelegatesTo(Project) Closure closure = {}) {
def project = ProjectBuilder.builder().build()
Expand All @@ -11,6 +14,24 @@ class Fixture {
project
}

/**
* Create a JAR file with given contents.
*
* @param destination JAR file
* @param contents map of filename and content
*/
static void createJAR(File destination, Map<String, String> contents) {
destination.withOutputStream {
new ZipOutputStream(it).withStream { zip ->
contents.each { filename, content ->
zip.putNextEntry(new ZipEntry(filename))
zip << content
zip.closeEntry()
}
}
}
}

static File file(YAML yaml) {
new File(Fixture.getResource("/${yaml}.yaml").path)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.hidetake.gradle.swagger.generator

import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

class ResolveSwaggerTemplateSpec extends Specification {

@Rule
TemporaryFolder temporaryFolder = new TemporaryFolder()

def 'task should expand JARs into build/swagger-template'() {
given:
def repositoryFolder = temporaryFolder.newFolder()
Fixture.createJAR(new File(repositoryFolder, 'template-foo-1.0.0.jar'), [
'foo.txt': 'foo-hello-world',
])
Fixture.createJAR(new File(repositoryFolder, 'template-bar-1.0.0.jar'), [
'bar.txt': 'bar-hello-world',
])

def project = Fixture.projectWithPlugin {
repositories {
flatDir {
dirs repositoryFolder
}
}
dependencies {
swaggerTemplate 'com.example:template-foo:1.0.0'
swaggerTemplate 'com.example:template-bar:1.0.0'
}
}

when:
project.tasks.resolveSwaggerTemplate.copy()

then:
project.file('build/swagger-template/foo.txt').text == 'foo-hello-world'
project.file('build/swagger-template/bar.txt').text == 'bar-hello-world'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SwaggerGeneratorPluginSpec extends Specification {
project.tasks.generateSwaggerUI
project.tasks.generateReDoc
project.tasks.validateSwagger
project.tasks.resolveSwaggerTemplate

and: 'should have default properties'
project.tasks.generateReDoc.scriptSrc =~ /\.js$/
Expand Down

0 comments on commit f8c7f30

Please sign in to comment.