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

View file

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