chore(tests): do not fail test on no connection

resolves #3860
This commit is contained in:
Jan De Dobbeleer 2023-05-17 20:03:57 +02:00 committed by Jan De Dobbeleer
parent c34e39c4ed
commit c62a272e72
5 changed files with 38 additions and 9 deletions

View file

@ -270,8 +270,12 @@ func escapeGlyphs(s string, migrate bool) string {
}
var cp codePoints
var err error
if migrate {
cp = getGlyphCodePoints()
cp, err = getGlyphCodePoints()
if err != nil {
migrate = false
}
}
var builder strings.Builder

View file

@ -45,6 +45,14 @@ import (
"golang.org/x/image/font"
)
type ConnectionError struct {
reason string
}
func (f *ConnectionError) Error() string {
return f.reason
}
const (
red = "#ED655A"
yellow = "#E1C04C"
@ -150,7 +158,7 @@ func (ir *ImageRenderer) Init(env platform.Environment) error {
ir.cleanContent()
if err := ir.loadFonts(); err != nil {
return err
return &ConnectionError{reason: err.Error()}
}
ir.defaultForegroundColor = &RGB{255, 255, 255}

View file

@ -48,17 +48,26 @@ func runImageTest(config, content string) (string, error) {
},
}
_ = image.Init(env)
err = image.Init(env)
if err != nil {
return "", err
}
err = image.SavePNG()
if err == nil {
os.Remove(image.Path)
}
return filepath.Base(image.Path), err
}
func TestStringImageFileWithText(t *testing.T) {
for _, tc := range cases {
filename, err := runImageTest(tc.Config, "foobar")
if connectionError, ok := err.(*ConnectionError); ok {
t.Log(connectionError.Error())
continue
}
assert.Equal(t, "jandedobbeleer.png", filename, tc.Case)
assert.NoError(t, err)
}
@ -68,6 +77,10 @@ func TestStringImageFileWithANSI(t *testing.T) {
prompt := ` jan  `
for _, tc := range cases {
filename, err := runImageTest(tc.Config, prompt)
if connectionError, ok := err.(*ConnectionError); ok {
t.Log(connectionError.Error())
continue
}
assert.Equal(t, "jandedobbeleer.png", filename, tc.Case)
assert.NoError(t, err)
}

View file

@ -10,7 +10,7 @@ import (
type codePoints map[int]int
func getGlyphCodePoints() codePoints {
func getGlyphCodePoints() (codePoints, error) {
var codePoints = make(codePoints)
client := &http.Client{}
@ -19,19 +19,19 @@ func getGlyphCodePoints() codePoints {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://ohmyposh.dev/codepoints.csv", nil)
if err != nil {
return codePoints
return codePoints, &ConnectionError{reason: err.Error()}
}
response, err := client.Do(request)
if err != nil {
return codePoints
return codePoints, err
}
defer response.Body.Close()
lines, err := csv.NewReader(response.Body).ReadAll()
if err != nil {
return codePoints
return codePoints, err
}
for _, line := range lines {
@ -48,5 +48,5 @@ func getGlyphCodePoints() codePoints {
}
codePoints[int(oldGlyph)] = int(newGlyph)
}
return codePoints
return codePoints, nil
}

View file

@ -7,6 +7,10 @@ import (
)
func TestGetCodePoints(t *testing.T) {
codepoints := getGlyphCodePoints()
codepoints, err := getGlyphCodePoints()
if connectionError, ok := err.(*ConnectionError); ok {
t.Log(connectionError.Error())
return
}
assert.Equal(t, 1939, len(codepoints))
}