11package segments
22
33import (
4+ "errors"
45 "io/fs"
56 "os"
67 "path/filepath"
78 "testing"
89
10+ "github.com/jandedobbeleer/oh-my-posh/src/runtime"
911 "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
1012 "github.com/jandedobbeleer/oh-my-posh/src/segments/options"
1113
@@ -642,6 +644,7 @@ func TestDotnetProject(t *testing.T) {
642644 },
643645 })
644646 env .On ("FileContent" , tc .FileName ).Return (tc .ProjectContents )
647+ env .On ("HasParentFilePath" , "Directory.Build.props" , false ).Return ((* runtime .FileInfo )(nil ), errors .New ("not found" ))
645648 pkg := & Project {}
646649 pkg .Init (options.Map {}, env )
647650 assert .Equal (t , tc .ExpectedEnabled , pkg .Enabled (), tc .Case )
@@ -653,13 +656,14 @@ func TestDotnetProject(t *testing.T) {
653656
654657func TestDotnetSolutionResolvesTargetFramework (t * testing.T ) {
655658 cases := []struct {
656- Case string
657- SolutionFile string
658- SubDirs map [string ][]fs.DirEntry // path -> entries returned by LsDir
659- FileContents map [string ]string // path -> file content
660- Options options.Map
661- ExpectedString string
662- ExpectedEnabled bool
659+ SubDirs map [string ][]fs.DirEntry
660+ FileContents map [string ]string
661+ Options options.Map
662+ DirectoryBuildProps * runtime.FileInfo
663+ Case string
664+ SolutionFile string
665+ ExpectedString string
666+ ExpectedEnabled bool
663667 }{
664668 {
665669 Case : ".sln with .csproj one level deep" ,
@@ -811,6 +815,49 @@ func TestDotnetSolutionResolvesTargetFramework(t *testing.T) {
811815 ExpectedEnabled : true ,
812816 ExpectedString : "MyApp" ,
813817 },
818+ {
819+ Case : "TFM defined centrally in Directory.Build.props" ,
820+ SolutionFile : "MyApp.sln" ,
821+ SubDirs : map [string ][]fs.DirEntry {
822+ "posh" : {
823+ & MockDirEntry {name : "MyApp.sln" },
824+ & MockDirEntry {name : "App" , isDir : true },
825+ },
826+ filepath .Join ("posh" , "App" ): {
827+ & MockDirEntry {name : "App.csproj" },
828+ },
829+ },
830+ FileContents : map [string ]string {
831+ "MyApp.sln" : "" ,
832+ filepath .Join ("App" , "App.csproj" ): "<Project><PropertyGroup></PropertyGroup></Project>" ,
833+ filepath .Join ("posh" , "Directory.Build.props" ): "<Project><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>" ,
834+ },
835+ DirectoryBuildProps : & runtime.FileInfo {
836+ Path : filepath .Join ("posh" , "Directory.Build.props" ),
837+ ParentFolder : "posh" ,
838+ },
839+ ExpectedEnabled : true ,
840+ ExpectedString : "MyApp net10.0" ,
841+ },
842+ {
843+ Case : "no TFM anywhere, Directory.Build.props not found" ,
844+ SolutionFile : "MyApp.sln" ,
845+ SubDirs : map [string ][]fs.DirEntry {
846+ "posh" : {
847+ & MockDirEntry {name : "MyApp.sln" },
848+ & MockDirEntry {name : "App" , isDir : true },
849+ },
850+ filepath .Join ("posh" , "App" ): {
851+ & MockDirEntry {name : "App.csproj" },
852+ },
853+ },
854+ FileContents : map [string ]string {
855+ "MyApp.sln" : "" ,
856+ filepath .Join ("App" , "App.csproj" ): "<Project><PropertyGroup></PropertyGroup></Project>" ,
857+ },
858+ ExpectedEnabled : true ,
859+ ExpectedString : "MyApp" ,
860+ },
814861 }
815862
816863 for _ , tc := range cases {
@@ -835,6 +882,12 @@ func TestDotnetSolutionResolvesTargetFramework(t *testing.T) {
835882 env .On ("FileContent" , path ).Return (content )
836883 }
837884
885+ if tc .DirectoryBuildProps != nil {
886+ env .On ("HasParentFilePath" , "Directory.Build.props" , false ).Return (tc .DirectoryBuildProps , nil )
887+ } else {
888+ env .On ("HasParentFilePath" , "Directory.Build.props" , false ).Return ((* runtime .FileInfo )(nil ), errors .New ("not found" ))
889+ }
890+
838891 opts := tc .Options
839892 if opts == nil {
840893 opts = options.Map {}
0 commit comments