Skip to content

Commit

Permalink
Add initial unit tests for script and config formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman committed Nov 12, 2024
1 parent 27886b2 commit 16e92d2
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/test/groovy/nextflow/lsp/TestLanguageClient.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2013-2024, Seqera Labs
*
* 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 nextflow.lsp

import java.util.concurrent.CompletableFuture

import org.eclipse.lsp4j.MessageActionItem
import org.eclipse.lsp4j.MessageParams
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.eclipse.lsp4j.ShowMessageRequestParams
import org.eclipse.lsp4j.services.LanguageClient

/**
*
* @author Ben Sherman <[email protected]>
*/
class TestLanguageClient implements LanguageClient {

@Override
public void telemetryEvent(Object object) {
}

@Override
public CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams requestParams) {
return null
}

@Override
public void showMessage(MessageParams messageParams) {
}

@Override
public void publishDiagnostics(PublishDiagnosticsParams diagnostics) {
}

@Override
public void logMessage(MessageParams message) {
}
}
2 changes: 1 addition & 1 deletion src/test/groovy/nextflow/lsp/file/FileCacheTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FileCacheTest extends Specification {
)
))
then:
'hello there, friend' == fileCache.getContents(URI.create('file.txt'))
'hello there, friend' == fileCache.getContents(URI.create('file.txt'))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2013-2024, Seqera Labs
*
* 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 nextflow.lsp.services.config

import java.nio.file.Files
import java.nio.file.Path

import nextflow.lsp.services.util.FormattingOptions
import nextflow.lsp.TestLanguageClient
import org.eclipse.lsp4j.DidOpenTextDocumentParams
import org.eclipse.lsp4j.Position
import org.eclipse.lsp4j.TextDocumentItem
import spock.lang.Specification

/**
*
* @author Ben Sherman <[email protected]>
*/
class ConfigFormattingTest extends Specification {

String openAndFormat(ConfigService service, Path filePath, String contents) {
def uri = filePath.toUri()
def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow-config', 1, contents)
service.didOpen(new DidOpenTextDocumentParams(textDocumentItem))
def textEdits = service.formatting(uri, new FormattingOptions(4, true, false))
return textEdits.first().getNewText()
}

def 'should format a config file' () {
given:
def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/')
if( !Files.exists(workspaceRoot) )
workspaceRoot.toFile().mkdirs()

def service = new ConfigService()
service.connect(new TestLanguageClient())
service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false)

when:
def filePath = workspaceRoot.resolve('nextflow.config')
def contents = '''\
process.cpus = 2 ; process.memory = 8.GB
'''.stripIndent()
then:
openAndFormat(service, filePath, contents) == '''\
process.cpus = 2
process.memory = 8.GB
'''.stripIndent()

when:
contents = '''\
process.cpus = 2
process.memory = 8.GB
'''.stripIndent()
then:
openAndFormat(service, filePath, contents) == '''\
process.cpus = 2
process.memory = 8.GB
'''.stripIndent()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2013-2024, Seqera Labs
*
* 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 nextflow.lsp.services.script

import java.nio.file.Files
import java.nio.file.Path

import nextflow.lsp.services.util.FormattingOptions
import nextflow.lsp.TestLanguageClient
import org.eclipse.lsp4j.DidOpenTextDocumentParams
import org.eclipse.lsp4j.Position
import org.eclipse.lsp4j.TextDocumentItem
import spock.lang.Specification

/**
*
* @author Ben Sherman <[email protected]>
*/
class ScriptFormattingTest extends Specification {

String openAndFormat(ScriptService service, Path filePath, String contents) {
def uri = filePath.toUri()
def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow', 1, contents)
service.didOpen(new DidOpenTextDocumentParams(textDocumentItem))
def textEdits = service.formatting(uri, new FormattingOptions(4, true, false))
return textEdits.first().getNewText()
}

def 'should format a script' () {
given:
def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/')
if( !Files.exists(workspaceRoot) )
workspaceRoot.toFile().mkdirs()

def service = new ScriptService()
service.connect(new TestLanguageClient())
service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false)

when:
def filePath = workspaceRoot.resolve('main.nf')
def contents = '''\
workflow { println 'Hello!' }
'''.stripIndent()
then:
openAndFormat(service, filePath, contents) == '''\
workflow {
println('Hello!')
}
'''.stripIndent()

when:
contents = '''\
workflow {
println('Hello!')
}
'''.stripIndent()
then:
openAndFormat(service, filePath, contents) == '''\
workflow {
println('Hello!')
}
'''.stripIndent()
}

}

0 comments on commit 16e92d2

Please sign in to comment.