Skip to content

Commit

Permalink
Issue #79 Added tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Oct 15, 2015
1 parent 74a44d7 commit 9ad6956
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/main/groovy/nextflow/scm/AssetManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class AssetManager {

def result = new ScriptFile(getMainScriptFile())
result.revisionInfo = getCurrentRevisionAndName()
result.repository = getGitRepositoryUrl()
result.repository = getGitConfigRemoteUrl()
result.localPath = localPath.toPath()

return result
Expand Down Expand Up @@ -703,13 +703,25 @@ class AssetManager {
return result ? result.name : null
}

/**
* Models revision information of a project repository.
*/
@Canonical
static class RevisionInfo {

/**
* Git commit ID
*/
String commitId

/**
* Git tab or branch name
*/
String revision

/**
* @return A formatted string containing the commitId and revision properties
*/
String toString() {

if( !commitId ) {
Expand Down
24 changes: 24 additions & 0 deletions src/main/groovy/nextflow/script/ScriptFile.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,44 @@ import nextflow.scm.AssetManager
@ToString(includeNames = true)
class ScriptFile {

/**
* Pipeline main script file
*/
Path main

/**
* (remote) project repository URL
*/
String repository

/**
* The
*/
AssetManager.RevisionInfo revisionInfo

/**
* (local) project repository path
*/
Path localPath

/**
* @return Directory where the main script file is stored
*/
Path getParent() { main?.parent }

/**
* @return Main script file content as a text string
*/
String getText() { main?.text }

/**
* @return Repository commitId
*/
String getCommitId() { revisionInfo?.commitId }

/**
* @return Repository tag or branch
*/
String getRevision() { revisionInfo?.revision }

ScriptFile( File file ) {
Expand Down
44 changes: 38 additions & 6 deletions src/main/groovy/nextflow/script/WorkflowMetadata.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

package nextflow.script

import java.nio.file.Path

import groovy.transform.CompileStatic
Expand All @@ -28,8 +27,8 @@ import groovy.transform.ToString
import groovy.util.logging.Slf4j
import nextflow.Const
import nextflow.util.Duration

/**
* Models workflow metadata properties and notification handler
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
Expand Down Expand Up @@ -69,18 +68,48 @@ class WorkflowMetadata {
owner.session.onShutdown { invokeOnComplete() }
}

void onComplete( Closure closure ) {
events << closure
/**
* Implements the following idiom in the pipeline script:
* <pre>
* window.onComplete {
* // do something
* }
* </pre>
*
* @param action The action handler
*/
void onComplete( Closure action ) {
events << action
}

@PackageScope
void registerConfigAction( Map config ) {
/**
* Implements the following idiom in the pipeline script:
* <pre>
* window.onComplete = {
* // do something
* }
* </pre>
*
* @param action The action handler
*/
void setOnComplete( Closure action ) {
events << action
}

private void registerConfigAction( Map config ) {
if( !config ) return
if( config.onComplete instanceof Closure ) {
events.add( (Closure)config.onComplete )
}
}

/**
* This method is required in order to allow the {@code onComplete} closure
* defined in the {@code nextflow.config} file to access the {@code workflow}
* object.
*
* @return The workflow object itself.
*/
def getWorkflow() { return this }

@PackageScope
Expand All @@ -99,6 +128,9 @@ class WorkflowMetadata {
}
}

/**
* @return Render the workflow properties
*/
String toString() {
def result = new StringBuilder()
result << 'repository: ' << repository
Expand Down
37 changes: 37 additions & 0 deletions src/test/groovy/nextflow/scm/AssetManagerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package nextflow.scm
import nextflow.exception.AbortOperationException
import org.eclipse.jgit.api.Git
import org.junit.Rule
import spock.lang.Requires
import spock.lang.Specification
Expand Down Expand Up @@ -317,5 +318,41 @@ class AssetManagerTest extends Specification {

}

def 'should create a script file object' () {

given:
def dir = tempDir.root
// create the repo dir
dir.resolve('main.nf').text = "println 'Hello world'"
dir.resolve('nextflow.config').text = 'manifest { }'

def init = Git.init()
def repo = init.setDirectory( dir.toFile() ).call()
repo.add().addFilepattern('.').call()
def commit = repo.commit().setAll(true).setMessage('First commit').call()
repo.close()

// append fake remote data
dir.resolve('.git/config') << '''
[remote "origin"]
url = https://github.com/nextflow-io/nextflow.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
'''
.stripIndent()

when:
def manager = new AssetManager().setLocalPath(dir.toFile())
def script = manager.getScriptFile()
then:
script.localPath == dir
script.commitId == commit.name().substring(0,10)
script.revision == 'master'
script.parent == dir
script.text == "println 'Hello world'"
script.repository == 'https://github.com/nextflow-io/nextflow.git'
}

}
115 changes: 115 additions & 0 deletions src/test/groovy/nextflow/script/WorkflowMetadataTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2013-2015, Centre for Genomic Regulation (CRG).
* Copyright (c) 2013-2015, Paolo Di Tommaso and the respective authors.
*
* This file is part of 'Nextflow'.
*
* Nextflow is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Nextflow is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nextflow. If not, see <http://www.gnu.org/licenses/>.
*/

package nextflow.script

import java.nio.file.Files

import nextflow.Const
import nextflow.Session
import nextflow.scm.AssetManager
import nextflow.util.Duration
import org.eclipse.jgit.api.Git
import spock.lang.Specification

/**
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
class WorkflowMetadataTest extends Specification {

def 'should populate workflow object' () {

given:
final begin = new Date()
def dir = Files.createTempDirectory('test')
/*
* create the github repository
*/
dir.resolve('main.nf').text = "println 'Hello world'"
dir.resolve('nextflow.config').text = 'manifest { }'

def init = Git.init()
def repo = init.setDirectory( dir.toFile() ).call()
repo.add().addFilepattern('.').call()
def commit = repo.commit().setAll(true).setMessage('First commit').call()
repo.close()

// append fake remote data
dir.resolve('.git/config') << '''
[remote "origin"]
url = https://github.com/nextflow-io/nextflow.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
'''
.stripIndent()

/*
* create ScriptFile object
*/
def manager = new AssetManager().setLocalPath(dir.toFile())
def script = manager.getScriptFile()

/*
* config file onComplete handler
*/
def handlerInvoked = false
def session = new Session([workflow: [onComplete: { -> handlerInvoked=true } ] ])

/*
* script runner
*/
def runner = Mock(ScriptRunner)
runner.getScriptFile() >> script
runner.fetchContainers() >> 'busybox/latest'
runner.commandLine >> 'nextflow run -this -that'
runner.session >> session

when:
def workflow = new WorkflowMetadata(runner)
then:
workflow.repository == 'https://github.com/nextflow-io/nextflow.git'
workflow.commitId == commit.name().substring(0,10)
workflow.revision == 'master'
workflow.container == 'busybox/latest'
workflow.localPath == dir
workflow.startTime >= begin
workflow.startTime <= new Date()
workflow.endTime == null
workflow.commandLine == 'nextflow run -this -that'
workflow.nextflow.version == Const.APP_VER
workflow.nextflow.build == Const.APP_BUILDNUM
workflow.nextflow.timestamp == Const.APP_TIMESTAMP_UTC

when:
workflow.invokeOnComplete()
then:
workflow.endTime > workflow.startTime
workflow.endTime <= new Date()
workflow.duration == new Duration( workflow.endTime.time - workflow.startTime.time )
handlerInvoked

cleanup:
dir?.deleteDir()
}

}
2 changes: 1 addition & 1 deletion tests

0 comments on commit 9ad6956

Please sign in to comment.