Skip to content

Commit 16e92d2

Browse files
committed
Add initial unit tests for script and config formatting
Signed-off-by: Ben Sherman <bentshermann@gmail.com>
1 parent 27886b2 commit 16e92d2

4 files changed

Lines changed: 209 additions & 1 deletion

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2013-2024, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package nextflow.lsp
18+
19+
import java.util.concurrent.CompletableFuture
20+
21+
import org.eclipse.lsp4j.MessageActionItem
22+
import org.eclipse.lsp4j.MessageParams
23+
import org.eclipse.lsp4j.PublishDiagnosticsParams
24+
import org.eclipse.lsp4j.ShowMessageRequestParams
25+
import org.eclipse.lsp4j.services.LanguageClient
26+
27+
/**
28+
*
29+
* @author Ben Sherman <bentshermann@gmail.com>
30+
*/
31+
class TestLanguageClient implements LanguageClient {
32+
33+
@Override
34+
public void telemetryEvent(Object object) {
35+
}
36+
37+
@Override
38+
public CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams requestParams) {
39+
return null
40+
}
41+
42+
@Override
43+
public void showMessage(MessageParams messageParams) {
44+
}
45+
46+
@Override
47+
public void publishDiagnostics(PublishDiagnosticsParams diagnostics) {
48+
}
49+
50+
@Override
51+
public void logMessage(MessageParams message) {
52+
}
53+
}

src/test/groovy/nextflow/lsp/file/FileCacheTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FileCacheTest extends Specification {
6767
)
6868
))
6969
then:
70-
'hello there, friend' == fileCache.getContents(URI.create('file.txt'))
70+
'hello there, friend' == fileCache.getContents(URI.create('file.txt'))
7171
}
7272

7373
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2013-2024, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package nextflow.lsp.services.config
18+
19+
import java.nio.file.Files
20+
import java.nio.file.Path
21+
22+
import nextflow.lsp.services.util.FormattingOptions
23+
import nextflow.lsp.TestLanguageClient
24+
import org.eclipse.lsp4j.DidOpenTextDocumentParams
25+
import org.eclipse.lsp4j.Position
26+
import org.eclipse.lsp4j.TextDocumentItem
27+
import spock.lang.Specification
28+
29+
/**
30+
*
31+
* @author Ben Sherman <bentshermann@gmail.com>
32+
*/
33+
class ConfigFormattingTest extends Specification {
34+
35+
String openAndFormat(ConfigService service, Path filePath, String contents) {
36+
def uri = filePath.toUri()
37+
def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow-config', 1, contents)
38+
service.didOpen(new DidOpenTextDocumentParams(textDocumentItem))
39+
def textEdits = service.formatting(uri, new FormattingOptions(4, true, false))
40+
return textEdits.first().getNewText()
41+
}
42+
43+
def 'should format a config file' () {
44+
given:
45+
def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/')
46+
if( !Files.exists(workspaceRoot) )
47+
workspaceRoot.toFile().mkdirs()
48+
49+
def service = new ConfigService()
50+
service.connect(new TestLanguageClient())
51+
service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false)
52+
53+
when:
54+
def filePath = workspaceRoot.resolve('nextflow.config')
55+
def contents = '''\
56+
process.cpus = 2 ; process.memory = 8.GB
57+
'''.stripIndent()
58+
then:
59+
openAndFormat(service, filePath, contents) == '''\
60+
process.cpus = 2
61+
process.memory = 8.GB
62+
'''.stripIndent()
63+
64+
when:
65+
contents = '''\
66+
process.cpus = 2
67+
process.memory = 8.GB
68+
'''.stripIndent()
69+
then:
70+
openAndFormat(service, filePath, contents) == '''\
71+
process.cpus = 2
72+
process.memory = 8.GB
73+
'''.stripIndent()
74+
}
75+
76+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2013-2024, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package nextflow.lsp.services.script
18+
19+
import java.nio.file.Files
20+
import java.nio.file.Path
21+
22+
import nextflow.lsp.services.util.FormattingOptions
23+
import nextflow.lsp.TestLanguageClient
24+
import org.eclipse.lsp4j.DidOpenTextDocumentParams
25+
import org.eclipse.lsp4j.Position
26+
import org.eclipse.lsp4j.TextDocumentItem
27+
import spock.lang.Specification
28+
29+
/**
30+
*
31+
* @author Ben Sherman <bentshermann@gmail.com>
32+
*/
33+
class ScriptFormattingTest extends Specification {
34+
35+
String openAndFormat(ScriptService service, Path filePath, String contents) {
36+
def uri = filePath.toUri()
37+
def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow', 1, contents)
38+
service.didOpen(new DidOpenTextDocumentParams(textDocumentItem))
39+
def textEdits = service.formatting(uri, new FormattingOptions(4, true, false))
40+
return textEdits.first().getNewText()
41+
}
42+
43+
def 'should format a script' () {
44+
given:
45+
def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/')
46+
if( !Files.exists(workspaceRoot) )
47+
workspaceRoot.toFile().mkdirs()
48+
49+
def service = new ScriptService()
50+
service.connect(new TestLanguageClient())
51+
service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false)
52+
53+
when:
54+
def filePath = workspaceRoot.resolve('main.nf')
55+
def contents = '''\
56+
workflow { println 'Hello!' }
57+
'''.stripIndent()
58+
then:
59+
openAndFormat(service, filePath, contents) == '''\
60+
workflow {
61+
println('Hello!')
62+
}
63+
'''.stripIndent()
64+
65+
when:
66+
contents = '''\
67+
workflow {
68+
println('Hello!')
69+
}
70+
'''.stripIndent()
71+
then:
72+
openAndFormat(service, filePath, contents) == '''\
73+
workflow {
74+
println('Hello!')
75+
}
76+
'''.stripIndent()
77+
}
78+
79+
}

0 commit comments

Comments
 (0)