fix(project): ignore dotnet target if not found

This commit is contained in:
Paulo Morgado 2023-11-01 11:36:37 +00:00 committed by Jan De Dobbeleer
parent f400bd3c45
commit dbc8998749
2 changed files with 15 additions and 8 deletions

View file

@ -3,7 +3,7 @@ package segments
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"path/filepath"
"strings"
@ -208,13 +208,14 @@ func (n *Project) getNuSpecPackage(_ ProjectItem) *ProjectData {
func (n *Project) getDotnetProject(_ ProjectItem) *ProjectData {
var name string
var content string
var extension string
extensions := []string{".sln", ".slnf", ".csproj", ".fsproj", ".vbproj"}
files := n.env.LsDir(n.env.Pwd())
// get the first match only
for _, file := range files {
extension := filepath.Ext(file.Name())
extension = filepath.Ext(file.Name())
if slices.Contains(extensions, extension) {
name = strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
content = n.env.FileContent(file.Name())
@ -224,13 +225,17 @@ func (n *Project) getDotnetProject(_ ProjectItem) *ProjectData {
// the name of the parameter may differ depending on the version,
// so instead of xml.Unmarshal() we use regex:
var target string
tag := "(?P<TAG><.*TargetFramework.*>(?P<TFM>.*)</.*TargetFramework.*>)"
values := regex.FindNamedRegexMatch(tag, content)
if len(values) == 0 {
n.Error = errors.New("cannot extract TFM from " + name + " project file").Error()
return nil
if len(values) != 0 {
target = values["TFM"]
}
if len(target) == 0 {
n.env.Error(fmt.Errorf("cannot extract TFM from %s project file", name))
}
target := values["TFM"]
return &ProjectData{
Target: target,

View file

@ -11,6 +11,7 @@ import (
"github.com/alecthomas/assert"
mock2 "github.com/stretchr/testify/mock"
testify_mock "github.com/stretchr/testify/mock"
)
@ -343,8 +344,8 @@ func TestDotnetProject(t *testing.T) {
Case: "invalid or empty contents",
FileName: "Invalid.csproj",
HasFiles: true,
ExpectedEnabled: false,
ExpectedString: "cannot extract TFM from Invalid project file",
ExpectedEnabled: true,
ExpectedString: "Invalid",
},
{
Case: "no files",
@ -370,6 +371,7 @@ func TestDotnetProject(t *testing.T) {
},
})
env.On("FileContent", tc.FileName).Return(tc.ProjectContents)
env.On("Error", mock2.Anything)
pkg := &Project{}
pkg.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, pkg.Enabled(), tc.Case)