Merge pull request #10 from Myzel394/add-cicd

Add ci-cd
This commit is contained in:
Myzel394 2024-08-11 19:27:36 +02:00 committed by GitHub
commit 3ebdc848e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 167 additions and 4 deletions

26
.github/workflows/pr-tests.yaml vendored Normal file
View File

@ -0,0 +1,26 @@
name: Run Go tests
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.22.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
# You can test your matrix by printing the current Go version
- name: Display Go version
run: go version
- name: Get dependencies
run: go mod download
- name: Run tests
run: go test -v ./...

View File

@ -0,0 +1,121 @@
package fstab
import (
fstabdocumentation "config-lsp/handlers/fstab/documentation"
"config-lsp/utils"
"testing"
)
var sampleValidBasicExample = `
LABEL=test /mnt/test ext4 defaults 0 0
`
var sampleInvalidOptionsExample = `
LABEL=test /mnt/test btrfs subvol=backup,fat=32 0 0
`
func TestValidBasicExample(t *testing.T) {
// Arrange
parser := FstabParser{}
errors := parser.ParseFromContent(sampleValidBasicExample)
if len(errors) > 0 {
t.Fatal("ParseFromContent failed with error", errors)
}
// Get hover for first field
entry := parser.entries[0]
println("Getting hover info")
{
hover, err := getHoverInfo(&entry, uint32(0))
if err != nil {
t.Fatal("getHoverInfo failed with error", err)
}
if hover.Contents != SpecHoverField.Contents {
t.Fatal("getHoverInfo failed to return correct hover content. Got:", hover.Contents, "but expected:", SpecHoverField.Contents)
}
// Get hover for second field
hover, err = getHoverInfo(&entry, uint32(11))
if err != nil {
t.Fatal("getHoverInfo failed with error", err)
}
if hover.Contents != MountPointHoverField.Contents {
t.Fatal("getHoverInfo failed to return correct hover content. Got:", hover.Contents, "but expected:", MountPointHoverField.Contents)
}
hover, err = getHoverInfo(&entry, uint32(20))
if err != nil {
t.Fatal("getHoverInfo failed with error", err)
}
if hover.Contents != MountPointHoverField.Contents {
t.Fatal("getHoverInfo failed to return correct hover content. Got:", hover.Contents, "but expected:", MountPointHoverField.Contents)
}
}
println("Getting completions")
{
completions, err := getCompletion(entry.Line, uint32(0))
if err != nil {
t.Fatal("getCompletion failed with error", err)
}
if len(completions) != 4 {
t.Fatal("getCompletion failed to return correct number of completions. Got:", len(completions), "but expected:", 4)
}
if completions[0].Label != "UUID" {
t.Fatal("getCompletion failed to return correct label. Got:", completions[0].Label, "but expected:", "UUID")
}
}
{
completions, err := getCompletion(entry.Line, uint32(21))
if err != nil {
t.Fatal("getCompletion failed with error", err)
}
expectedLength := len(utils.KeysOfMap(fstabdocumentation.MountOptionsMapField))
if len(completions) != expectedLength {
t.Fatal("getCompletion failed to return correct number of completions. Got:", len(completions), "but expected:", expectedLength)
}
}
println("Checking values")
{
diagnostics := parser.AnalyzeValues()
if len(diagnostics) > 0 {
t.Fatal("AnalyzeValues failed with error", diagnostics)
}
}
}
func TestInvalidOptionsExample(t *testing.T) {
// Arrange
parser := FstabParser{}
errors := parser.ParseFromContent(sampleInvalidOptionsExample)
if len(errors) > 0 {
t.Fatal("ParseFromContent returned error", errors)
}
// Get hover for first field
println("Checking values")
{
diagnostics := parser.AnalyzeValues()
if len(diagnostics) == 0 {
t.Fatal("AnalyzeValues should have returned error")
}
}
}

View File

@ -21,8 +21,20 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
), nil
}
if entry.Type == FstabEntryTypeComment {
return nil, nil
}
cursor := params.Position.Character
line := entry.Line
return getCompletion(line, cursor)
}
func getCompletion(
line FstabLine,
cursor uint32,
) ([]protocol.CompletionItem, error) {
targetField := line.GetFieldAtPosition(cursor)
switch targetField {

View File

@ -26,6 +26,10 @@ func TextDocumentHover(context *glsp.Context, params *protocol.HoverParams) (*pr
return nil, nil
}
return getHoverInfo(entry, cursor)
}
func getHoverInfo(entry *FstabEntry, cursor uint32) (*protocol.Hover, error) {
line := entry.Line
targetField := line.GetFieldAtPosition(cursor)

View File

@ -27,7 +27,7 @@ func (v DataAmountValue) CheckIsValid(value string) []*docvalues.InvalidValue {
if !dataAmountCheckPattern.MatchString(value) {
return []*docvalues.InvalidValue{
{
Err: InvalidDataAmountError{},
Err: InvalidDataAmountError{},
Start: 0,
End: uint32(len(value)),
},

View File

@ -29,7 +29,7 @@ func (v TimeFormatValue) CheckIsValid(value string) []*docvalues.InvalidValue {
if !timeFormatCheckPattern.MatchString(value) {
return []*docvalues.InvalidValue{
{
Err: InvalidTimeFormatError{},
Err: InvalidTimeFormatError{},
Start: 0,
End: uint32(len(value)),
},

View File

@ -28,7 +28,7 @@ type FatalFileNotReadableError struct {
}
func (e FatalFileNotReadableError) Error() string {
return fmt.Sprint("Fatal error! config-lsp was unable to read the file (%s); error: %s", e.FileURI, e.Err.Error())
return fmt.Sprintf("Fatal error! config-lsp was unable to read the file (%s); error: %s", e.FileURI, e.Err.Error())
}
type UnsupportedLanguageError struct {
@ -36,7 +36,7 @@ type UnsupportedLanguageError struct {
}
func (e UnsupportedLanguageError) Error() string {
return fmt.Sprint("Language '%s' is not supported. Choose one of: %s", e.SuggestedLanguage, strings.Join(AllSupportedLanguages, ", "))
return fmt.Sprintf("Language '%s' is not supported. Choose one of: %s", e.SuggestedLanguage, strings.Join(AllSupportedLanguages, ", "))
}
type LanguageUndetectableError struct{}