Skip to content

Commit

Permalink
add asdfparser tests
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Vaumoron <[email protected]>
  • Loading branch information
dvaumoron committed Dec 22, 2024
1 parent 5116d09 commit cd5eb15
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
11 changes: 8 additions & 3 deletions versionmanager/semantic/parser/asdf/asdfparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package asdfparser
import (
"bufio"
"errors"
"io"
"io/fs"
"os"
"strings"
Expand Down Expand Up @@ -59,8 +60,12 @@ func retrieveVersionFromToolFile(filePath, toolName string, conf *config.Config)
}
defer file.Close()

return parseVersionFromToolFileReader(filePath, file, toolName, conf.Displayer)
}

func parseVersionFromToolFileReader(filePath string, reader io.Reader, toolName string, displayer loghelper.Displayer) (string, error) {

Check failure on line 66 in versionmanager/semantic/parser/asdf/asdfparser.go

View workflow job for this annotation

GitHub Actions / lint

parseVersionFromToolFileReader - result 1 (error) is always nil (unparam)
resolvedVersion := ""
scanner := bufio.NewScanner(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
trimmedLine := strings.TrimSpace(scanner.Text())

Expand All @@ -78,7 +83,7 @@ func retrieveVersionFromToolFile(filePath, toolName string, conf *config.Config)
}

if err := scanner.Err(); err != nil {
conf.Displayer.Log(hclog.Warn, "Failed to parse tool file", loghelper.Error, err)
displayer.Log(hclog.Warn, "Failed to parse tool file", loghelper.Error, err)

return "", nil
}
Expand All @@ -87,5 +92,5 @@ func retrieveVersionFromToolFile(filePath, toolName string, conf *config.Config)
return "", nil
}

return types.DisplayDetectionInfo(conf.Displayer, resolvedVersion, filePath), nil
return types.DisplayDetectionInfo(displayer, resolvedVersion, filePath), nil
}
56 changes: 56 additions & 0 deletions versionmanager/semantic/parser/asdf/asdfparser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* 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 asdfparser

import (
"bytes"
_ "embed"
"testing"

"github.com/tofuutils/tenv/v3/pkg/loghelper"
)

//go:embed testdata/.tool-versions
var toolFileData []byte

func TestParseVersionFromToolFileReader(t *testing.T) {

Check failure on line 32 in versionmanager/semantic/parser/asdf/asdfparser_test.go

View workflow job for this annotation

GitHub Actions / lint

TestParseVersionFromToolFileReader's subtests should call t.Parallel (tparallel)
t.Parallel()

t.Run("BasicLine", func(t *testing.T) {

Check failure on line 35 in versionmanager/semantic/parser/asdf/asdfparser_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestParseVersionFromToolFileReader missing the call to method parallel in the test run (paralleltest)
version, err := parseVersionFromToolFileReader("", bytes.NewReader(toolFileData), "nodejs", loghelper.InertDisplayer)
if err != nil {
t.Fatal("Unexpected parse error : ", err)
}

if version != "10.15.0" {
t.Fatal("Unexpected version : ", version)
}
})

t.Run("LineWithComment", func(t *testing.T) {

Check failure on line 46 in versionmanager/semantic/parser/asdf/asdfparser_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestParseVersionFromToolFileReader missing the call to method parallel in the test run (paralleltest)
version, err := parseVersionFromToolFileReader("", bytes.NewReader(toolFileData), "ruby", loghelper.InertDisplayer)
if err != nil {
t.Fatal("Unexpected parse error : ", err)
}

if version != "2.5.3" {
t.Fatal("Unexpected version : ", version)
}
})
}
3 changes: 3 additions & 0 deletions versionmanager/semantic/parser/asdf/testdata/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ruby 2.5.3 # This is a comment
# This is another comment
nodejs 10.15.0

0 comments on commit cd5eb15

Please sign in to comment.