-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from github/rework-tmp-assets
Refactor font handling and embed assets instead of relative path
- Loading branch information
Showing
9 changed files
with
189 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package geometry | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/github/gh-skyline/errors" | ||
) | ||
|
||
//go:embed assets/* | ||
var embeddedAssets embed.FS | ||
|
||
// writeTempFont writes the embedded font to a temporary file and returns its path. | ||
// The caller is responsible for cleaning up the temporary file. | ||
func writeTempFont(fontName string) (string, func(), error) { | ||
fontBytes, err := embeddedAssets.ReadFile("assets/" + fontName) | ||
if err != nil { | ||
return "", nil, errors.New(errors.IOError, "failed to read embedded font", err) | ||
} | ||
|
||
// Create temp file with .ttf extension to ensure proper font loading | ||
tmpFile, err := os.CreateTemp("", "skyline-font-*.ttf") | ||
if err != nil { | ||
return "", nil, errors.New(errors.IOError, "failed to create temp font file", err) | ||
} | ||
|
||
if _, err := tmpFile.Write(fontBytes); err != nil { | ||
closeErr := tmpFile.Close() | ||
removeErr := os.Remove(tmpFile.Name()) | ||
return "", nil, errors.New(errors.IOError, "failed to write font to temp file", fmt.Errorf("%w; close error: %v; remove error: %v", err, closeErr, removeErr)) | ||
} | ||
if err := tmpFile.Close(); err != nil { | ||
removeErr := os.Remove(tmpFile.Name()) | ||
return "", nil, errors.New(errors.IOError, "failed to close temp font file", fmt.Errorf("%w; remove error: %v", err, removeErr)) | ||
} | ||
|
||
cleanup := func() { | ||
_ = os.Remove(tmpFile.Name()) // Ignore cleanup errors in defer | ||
} | ||
|
||
return tmpFile.Name(), cleanup, nil | ||
} | ||
|
||
// getEmbeddedImage returns a temporary file path for the embedded image. | ||
// The caller is responsible for cleaning up the temporary file. | ||
func getEmbeddedImage() (string, func(), error) { | ||
imgBytes, err := embeddedAssets.ReadFile("assets/invertocat.png") | ||
if err != nil { | ||
return "", nil, errors.New(errors.IOError, "failed to read embedded image", err) | ||
} | ||
|
||
tmpFile, err := os.CreateTemp("", "skyline-img-*.png") | ||
if err != nil { | ||
return "", nil, errors.New(errors.IOError, "failed to create temp image file", err) | ||
} | ||
|
||
if _, err := tmpFile.Write(imgBytes); err != nil { | ||
closeErr := tmpFile.Close() | ||
removeErr := os.Remove(tmpFile.Name()) | ||
return "", nil, errors.New(errors.IOError, "failed to write image to temp file", fmt.Errorf("%w; close error: %v; remove error: %v", err, closeErr, removeErr)) | ||
} | ||
if err := tmpFile.Close(); err != nil { | ||
removeErr := os.Remove(tmpFile.Name()) | ||
return "", nil, errors.New(errors.IOError, "failed to close temp image file", fmt.Errorf("%w; remove error: %v", err, removeErr)) | ||
} | ||
|
||
cleanup := func() { | ||
_ = os.Remove(tmpFile.Name()) // Ignore cleanup errors in defer | ||
} | ||
|
||
return tmpFile.Name(), cleanup, nil | ||
} |
File renamed without changes
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package geometry | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
// TestWriteTempFont verifies temporary font file creation and cleanup | ||
func TestWriteTempFont(t *testing.T) { | ||
t.Run("verify valid font extraction", func(t *testing.T) { | ||
fontPath, cleanup, err := writeTempFont("monasans-medium.ttf") | ||
if err != nil { | ||
t.Fatalf("writeTempFont failed: %v", err) | ||
} | ||
defer cleanup() | ||
|
||
// Verify file exists and has content | ||
content, err := os.ReadFile(fontPath) | ||
if err != nil { | ||
t.Errorf("Failed to read temp font file: %v", err) | ||
} | ||
if len(content) == 0 { | ||
t.Error("Temp font file is empty") | ||
} | ||
|
||
// Verify file extension | ||
if filepath.Ext(fontPath) != ".ttf" { | ||
t.Errorf("Expected .ttf extension, got %s", filepath.Ext(fontPath)) | ||
} | ||
|
||
// Verify cleanup works | ||
cleanup() | ||
if _, err := os.Stat(fontPath); !os.IsNotExist(err) { | ||
t.Error("Temp font file not cleaned up properly") | ||
} | ||
}) | ||
|
||
t.Run("verify nonexistent font handling", func(t *testing.T) { | ||
_, cleanup, err := writeTempFont("nonexistent.ttf") | ||
if err == nil { | ||
defer cleanup() | ||
t.Error("Expected error for nonexistent font") | ||
} | ||
}) | ||
} | ||
|
||
// TestGetEmbeddedImage verifies temporary image file creation and cleanup | ||
func TestGetEmbeddedImage(t *testing.T) { | ||
t.Run("verify valid image extraction", func(t *testing.T) { | ||
imagePath, cleanup, err := getEmbeddedImage() | ||
if err != nil { | ||
t.Fatalf("getEmbeddedImage failed: %v", err) | ||
} | ||
defer cleanup() | ||
|
||
// Verify file exists and has content | ||
content, err := os.ReadFile(imagePath) | ||
if err != nil { | ||
t.Errorf("Failed to read temp image file: %v", err) | ||
} | ||
if len(content) == 0 { | ||
t.Error("Temp image file is empty") | ||
} | ||
|
||
// Verify file extension | ||
if filepath.Ext(imagePath) != ".png" { | ||
t.Errorf("Expected .png extension, got %s", filepath.Ext(imagePath)) | ||
} | ||
|
||
// Verify cleanup works | ||
cleanup() | ||
if _, err := os.Stat(imagePath); !os.IsNotExist(err) { | ||
t.Error("Temp image file not cleaned up properly") | ||
} | ||
}) | ||
|
||
// Test embedded filesystem access | ||
t.Run("verify embedded filesystem access", func(t *testing.T) { | ||
// Try to read the embedded image directly | ||
_, err := embeddedAssets.ReadFile("assets/invertocat.png") | ||
if err != nil { | ||
t.Errorf("Failed to access embedded image: %v", err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters