2023-02-26 06:37:37 -08:00
|
|
|
package segments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
testify_ "github.com/stretchr/testify/mock"
|
2023-02-26 06:37:37 -08:00
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
2023-02-26 06:37:37 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
2024-07-02 03:02:57 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
2024-07-03 00:04:11 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
2023-02-26 06:37:37 -08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CacheGet struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
found bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type CacheSet struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPResponse struct {
|
|
|
|
body string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnitySegment(t *testing.T) {
|
2023-03-03 23:29:47 -08:00
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
ExpectedOutput string
|
|
|
|
VersionFileText string
|
|
|
|
ExpectedToBeEnabled bool
|
|
|
|
VersionFileExists bool
|
|
|
|
}{
|
2023-04-14 07:13:50 -07:00
|
|
|
{
|
|
|
|
Case: "Unity version without f suffix",
|
|
|
|
ExpectedOutput: "\ue721 2023.2.0a9 C# 9",
|
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
|
|
|
VersionFileText: "m_EditorVersion: 2023.2.0a9\nm_EditorVersionWithRevision: 2023.2.0a9 (5405d0db74a0)",
|
|
|
|
},
|
2023-03-03 23:29:47 -08:00
|
|
|
{
|
|
|
|
Case: "Unity version exists in C# map",
|
|
|
|
ExpectedOutput: "\ue721 2021.3.16 C# 9",
|
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
|
|
|
VersionFileText: "m_EditorVersion: 2021.3.16f1\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "ProjectSettings/ProjectVersion.txt doesn't exist",
|
|
|
|
ExpectedToBeEnabled: false,
|
|
|
|
VersionFileExists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "ProjectSettings/ProjectVersion.txt is empty",
|
|
|
|
ExpectedToBeEnabled: false,
|
|
|
|
VersionFileExists: true,
|
|
|
|
VersionFileText: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "ProjectSettings/ProjectVersion.txt does not have expected format",
|
|
|
|
ExpectedToBeEnabled: false,
|
|
|
|
VersionFileExists: true,
|
|
|
|
VersionFileText: "2021.3.16f1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "CRLF line ending",
|
|
|
|
ExpectedOutput: "\ue721 2021.3.16 C# 9",
|
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
|
|
|
VersionFileText: "m_EditorVersion: 2021.3.16f1\r\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)\r\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2024-07-03 00:04:11 -07:00
|
|
|
env := new(mock.Environment)
|
|
|
|
env.On("Error", testify_.Anything).Return()
|
|
|
|
env.On("Debug", testify_.Anything)
|
2023-03-03 23:29:47 -08:00
|
|
|
|
|
|
|
err := errors.New("no match at root level")
|
2024-07-02 03:02:57 -07:00
|
|
|
var projectDir *runtime.FileInfo
|
2023-03-03 23:29:47 -08:00
|
|
|
if tc.VersionFileExists {
|
|
|
|
err = nil
|
2024-07-02 03:02:57 -07:00
|
|
|
projectDir = &runtime.FileInfo{
|
2023-03-03 23:29:47 -08:00
|
|
|
ParentFolder: "UnityProjectRoot",
|
|
|
|
Path: "UnityProjectRoot/ProjectSettings",
|
|
|
|
IsDir: true,
|
|
|
|
}
|
|
|
|
env.On("HasFilesInDir", projectDir.Path, "ProjectVersion.txt").Return(tc.VersionFileExists)
|
|
|
|
versionFilePath := filepath.Join(projectDir.Path, "ProjectVersion.txt")
|
|
|
|
env.On("FileContent", versionFilePath).Return(tc.VersionFileText)
|
|
|
|
}
|
|
|
|
env.On("HasParentFilePath", "ProjectSettings").Return(projectDir, err)
|
|
|
|
|
|
|
|
props := properties.Map{}
|
|
|
|
unity := &Unity{}
|
|
|
|
unity.Init(props, env)
|
|
|
|
assert.Equal(t, tc.ExpectedToBeEnabled, unity.Enabled())
|
|
|
|
if tc.ExpectedToBeEnabled {
|
|
|
|
assert.Equal(t, tc.ExpectedOutput, renderTemplate(env, unity.Template(), unity), tc.Case)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2021.9.20f1 is used in the test cases below as a fake Unity version.
|
2024-04-29 06:04:14 -07:00
|
|
|
// As such, it doesn't exist in the predefined map in unity.go. This
|
2023-03-03 23:29:47 -08:00
|
|
|
// allows us to test the web request portion of the code, which is the
|
|
|
|
// fallback for obtaining a C# version.
|
|
|
|
func TestUnitySegmentCSharpWebRequest(t *testing.T) {
|
2023-02-26 06:37:37 -08:00
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
ExpectedOutput string
|
|
|
|
VersionFileText string
|
|
|
|
CacheGet CacheGet
|
|
|
|
CacheSet CacheSet
|
|
|
|
ExpectedToBeEnabled bool
|
|
|
|
VersionFileExists bool
|
|
|
|
HTTPResponse HTTPResponse
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Case: "C# version cached",
|
2023-03-03 23:29:47 -08:00
|
|
|
ExpectedOutput: "\ue721 2021.9.20 C# 10",
|
2023-02-26 06:37:37 -08:00
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
2023-03-03 23:29:47 -08:00
|
|
|
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
2023-02-26 06:37:37 -08:00
|
|
|
CacheGet: CacheGet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
|
|
|
val: "C# 10",
|
2023-02-26 06:37:37 -08:00
|
|
|
found: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "C# version not cached",
|
2023-03-03 23:29:47 -08:00
|
|
|
ExpectedOutput: "\ue721 2021.9.20 C# 10",
|
2023-02-26 06:37:37 -08:00
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
2023-03-03 23:29:47 -08:00
|
|
|
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
2023-02-26 06:37:37 -08:00
|
|
|
CacheGet: CacheGet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
2023-02-26 06:37:37 -08:00
|
|
|
val: "",
|
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
CacheSet: CacheSet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
|
|
|
val: "C# 10",
|
2023-02-26 06:37:37 -08:00
|
|
|
},
|
|
|
|
HTTPResponse: HTTPResponse{
|
2023-03-03 23:29:47 -08:00
|
|
|
body: `<a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10">C# 10.0</a>`,
|
2023-02-26 06:37:37 -08:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "C# version has a minor version",
|
2023-03-03 23:29:47 -08:00
|
|
|
ExpectedOutput: "\ue721 2021.9.20 C# 10.1",
|
2023-02-26 06:37:37 -08:00
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
2023-03-03 23:29:47 -08:00
|
|
|
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
2023-02-26 06:37:37 -08:00
|
|
|
CacheGet: CacheGet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
2023-02-26 06:37:37 -08:00
|
|
|
val: "",
|
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
CacheSet: CacheSet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
|
|
|
val: "C# 10.1",
|
2023-02-26 06:37:37 -08:00
|
|
|
},
|
|
|
|
HTTPResponse: HTTPResponse{
|
2023-03-03 23:29:47 -08:00
|
|
|
body: `<a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10-1">C# 10.1</a>`,
|
2023-02-26 06:37:37 -08:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "C# version not found in webpage",
|
2023-03-03 23:29:47 -08:00
|
|
|
ExpectedOutput: "\ue721 2021.9.20",
|
2023-02-26 06:37:37 -08:00
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
2023-03-03 23:29:47 -08:00
|
|
|
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
2023-02-26 06:37:37 -08:00
|
|
|
CacheGet: CacheGet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
2023-02-26 06:37:37 -08:00
|
|
|
val: "",
|
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
CacheSet: CacheSet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
2023-02-26 06:37:37 -08:00
|
|
|
val: "",
|
|
|
|
},
|
|
|
|
HTTPResponse: HTTPResponse{
|
|
|
|
body: `<h1>Sorry... that page seems to be missing!</h1>`,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "http request fails",
|
2023-03-03 23:29:47 -08:00
|
|
|
ExpectedOutput: "\ue721 2021.9.20",
|
2023-02-26 06:37:37 -08:00
|
|
|
ExpectedToBeEnabled: true,
|
|
|
|
VersionFileExists: true,
|
2023-03-03 23:29:47 -08:00
|
|
|
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
2023-02-26 06:37:37 -08:00
|
|
|
CacheGet: CacheGet{
|
2023-03-03 23:29:47 -08:00
|
|
|
key: "2021.9",
|
2023-02-26 06:37:37 -08:00
|
|
|
val: "",
|
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
HTTPResponse: HTTPResponse{
|
|
|
|
body: "",
|
|
|
|
err: errors.New("FAIL"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2024-07-03 00:04:11 -07:00
|
|
|
env := new(mock.Environment)
|
|
|
|
env.On("Error", testify_.Anything).Return()
|
|
|
|
env.On("Debug", testify_.Anything)
|
2023-02-26 06:37:37 -08:00
|
|
|
|
|
|
|
err := errors.New("no match at root level")
|
2024-07-02 03:02:57 -07:00
|
|
|
var projectDir *runtime.FileInfo
|
2023-02-26 06:37:37 -08:00
|
|
|
if tc.VersionFileExists {
|
|
|
|
err = nil
|
2024-07-02 03:02:57 -07:00
|
|
|
projectDir = &runtime.FileInfo{
|
2023-02-26 06:37:37 -08:00
|
|
|
ParentFolder: "UnityProjectRoot",
|
|
|
|
Path: "UnityProjectRoot/ProjectSettings",
|
|
|
|
IsDir: true,
|
|
|
|
}
|
|
|
|
env.On("HasFilesInDir", projectDir.Path, "ProjectVersion.txt").Return(tc.VersionFileExists)
|
|
|
|
versionFilePath := filepath.Join(projectDir.Path, "ProjectVersion.txt")
|
|
|
|
env.On("FileContent", versionFilePath).Return(tc.VersionFileText)
|
|
|
|
}
|
|
|
|
env.On("HasParentFilePath", "ProjectSettings").Return(projectDir, err)
|
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
cache := &cache_.Cache{}
|
2023-02-26 06:37:37 -08:00
|
|
|
cache.On("Get", tc.CacheGet.key).Return(tc.CacheGet.val, tc.CacheGet.found)
|
|
|
|
cache.On("Set", tc.CacheSet.key, tc.CacheSet.val, -1).Return()
|
|
|
|
env.On("Cache").Return(cache)
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://docs.unity3d.com/%s/Documentation/Manual/CSharpCompiler.html", tc.CacheGet.key)
|
|
|
|
env.On("HTTPRequest", url).Return([]byte(tc.HTTPResponse.body), tc.HTTPResponse.err)
|
|
|
|
|
|
|
|
props := properties.Map{}
|
|
|
|
unity := &Unity{}
|
|
|
|
unity.Init(props, env)
|
|
|
|
assert.Equal(t, tc.ExpectedToBeEnabled, unity.Enabled())
|
|
|
|
if tc.ExpectedToBeEnabled {
|
|
|
|
assert.Equal(t, tc.ExpectedOutput, renderTemplate(env, unity.Template(), unity), tc.Case)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|