Skip to content

Commit 46ab32a

Browse files
committed
Add unit tests for LanguageService
Signed-off-by: Ben Sherman <bentshermann@gmail.com>
1 parent a519a18 commit 46ab32a

5 files changed

Lines changed: 627 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2024-2025, 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 nextflow.lsp.services.ErrorReportingMode
20+
import nextflow.lsp.services.LanguageServerConfiguration
21+
import nextflow.lsp.services.config.ConfigService
22+
import nextflow.lsp.spec.PluginSpecCache
23+
import org.eclipse.lsp4j.DiagnosticSeverity
24+
import spock.lang.Specification
25+
26+
import static nextflow.lsp.TestUtils.*
27+
28+
/**
29+
* Tests for diagnostic publishing in the base LanguageService:
30+
* error-reporting-mode filtering, severity mapping, and clearDiagnostics().
31+
*
32+
* @author Ben Sherman <bentshermann@gmail.com>
33+
*/
34+
class DiagnosticsReportingTest extends Specification {
35+
36+
// a config with an unrecognized option -> warning
37+
private static final String WARNING_SOURCE = "wokDir = 'work'\n"
38+
// a config with a syntax error -> error
39+
private static final String ERROR_SOURCE = 'process {\n'
40+
41+
private ConfigService serviceWithMode(TestLanguageClient client, ErrorReportingMode mode) {
42+
def d = LanguageServerConfiguration.defaults()
43+
def configuration = new LanguageServerConfiguration(
44+
d.dagDirection(), d.dagVerbose(), mode, d.excludePatterns(),
45+
d.extendedCompletion(), d.harshilAlignment(), d.maheshForm(),
46+
d.maxCompletionItems(), d.pluginRegistryUrl(), d.sortDeclarations())
47+
def service = new ConfigService(workspaceRootUri())
48+
service.connect(client)
49+
service.initialize(configuration, new PluginSpecCache(configuration.pluginRegistryUrl()))
50+
// mirror TestUtils.getConfigService: open empty file to skip the workspace scan
51+
open(service, getUri('nextflow.config'), '')
52+
service.updateNow()
53+
return service
54+
}
55+
56+
def 'OFF should suppress all diagnostics' () {
57+
given:
58+
def client = new TestLanguageClient()
59+
def service = serviceWithMode(client, ErrorReportingMode.OFF)
60+
def uri = getUri('nextflow.config')
61+
62+
when:
63+
open(service, uri, ERROR_SOURCE)
64+
service.updateNow()
65+
then:
66+
client.getDiagnostics(uri).isEmpty()
67+
}
68+
69+
def 'ERRORS should report errors but suppress warnings' () {
70+
given:
71+
def client = new TestLanguageClient()
72+
def service = serviceWithMode(client, ErrorReportingMode.ERRORS)
73+
def uri = getUri('nextflow.config')
74+
75+
when: 'a syntax error'
76+
open(service, uri, ERROR_SOURCE)
77+
service.updateNow()
78+
then:
79+
client.getDiagnostics(uri).any { it.getSeverity() == DiagnosticSeverity.Error }
80+
81+
when: 'an unrecognized-option warning'
82+
open(service, uri, WARNING_SOURCE)
83+
service.updateNow()
84+
then:
85+
client.getDiagnostics(uri).isEmpty()
86+
}
87+
88+
def 'WARNINGS should report both errors and warnings' () {
89+
given:
90+
def client = new TestLanguageClient()
91+
def service = serviceWithMode(client, ErrorReportingMode.WARNINGS)
92+
def uri = getUri('nextflow.config')
93+
94+
when: 'an unrecognized-option warning'
95+
open(service, uri, WARNING_SOURCE)
96+
service.updateNow()
97+
then:
98+
client.getDiagnostics(uri).any { it.getSeverity() == DiagnosticSeverity.Warning }
99+
100+
when: 'a syntax error'
101+
open(service, uri, ERROR_SOURCE)
102+
service.updateNow()
103+
then:
104+
client.getDiagnostics(uri).any { it.getSeverity() == DiagnosticSeverity.Error }
105+
}
106+
107+
def 'every diagnostic should be sourced to nextflow' () {
108+
given:
109+
def client = new TestLanguageClient()
110+
def service = serviceWithMode(client, ErrorReportingMode.WARNINGS)
111+
def uri = getUri('nextflow.config')
112+
113+
when:
114+
open(service, uri, WARNING_SOURCE)
115+
service.updateNow()
116+
then:
117+
def diagnostics = client.getDiagnostics(uri)
118+
!diagnostics.isEmpty()
119+
diagnostics.every { it.getSource() == 'nextflow' }
120+
}
121+
122+
def 'clearDiagnostics should publish empty diagnostics for known files' () {
123+
given:
124+
def client = new TestLanguageClient()
125+
def service = serviceWithMode(client, ErrorReportingMode.WARNINGS)
126+
def uri = getUri('nextflow.config')
127+
128+
when:
129+
open(service, uri, ERROR_SOURCE)
130+
service.updateNow()
131+
then:
132+
!client.getDiagnostics(uri).isEmpty()
133+
134+
when:
135+
service.clearDiagnostics()
136+
then:
137+
client.getDiagnostics(uri).isEmpty()
138+
}
139+
140+
}

0 commit comments

Comments
 (0)