Myzel394 98f76fd839
fix(server): Improve structure analyzer for ssh_config
Signed-off-by: Myzel394 <github.7a2op@simplelogin.co>
2025-03-16 00:23:54 +01:00

131 lines
2.9 KiB
Go

package analyzer
import (
testutils_test "config-lsp/handlers/ssh_config/test_utils"
"testing"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func testQuotes(
ctx *analyzerContext,
) {
for _, info := range ctx.document.Config.GetAllOptions() {
checkIsUsingDoubleQuotes(ctx, info.Option.Key.Value, info.Option.Key.LocationRange)
checkIsUsingDoubleQuotes(ctx, info.Option.OptionValue.Value, info.Option.OptionValue.LocationRange)
checkQuotesAreClosed(ctx, info.Option.Key.Value, info.Option.Key.LocationRange)
checkQuotesAreClosed(ctx, info.Option.OptionValue.Value, info.Option.OptionValue.LocationRange)
}
}
func TestSimpleInvalidQuotesExample(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
PermitRootLogin 'yes'
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}
testQuotes(ctx)
if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
}
}
func TestSingleQuotesKeyAndOptionExample(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
'Port' '22'
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}
testQuotes(ctx)
if !(len(ctx.diagnostics) == 2) {
t.Errorf("Expected 2 ctx.diagnostics, got %v", len(ctx.diagnostics))
}
}
func TestSimpleUnclosedQuoteExample(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
PermitRootLogin "yes
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}
testQuotes(ctx)
if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
}
}
func TestIncompleteQuotesExample(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
"Port
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}
testQuotes(ctx)
if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
}
}
func TestDependentOptionsExample(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
Port 1234
CanonicalDomains example.com
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}
option := d.FindOptionsByName("canonicaldomains")[0]
checkIsDependent(ctx, option.Option.Key, option.Block)
if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
}
}
func TestValidDependentOptionsExample(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
Port 1234
CanonicalizeHostname yes
CanonicalDomains "example.com"
Test "hello world 'test' "
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}
option := d.FindOptionsByName("canonicaldomains")[0]
checkIsDependent(ctx, option.Option.Key, option.Block)
if len(ctx.diagnostics) > 0 {
t.Errorf("Expected no errors, got %v", len(ctx.diagnostics))
}
}