mirror of
https://github.com/ivuorinen/gh-action-readme.git
synced 2026-02-15 00:49:46 +00:00
feat(lint): add many linters, make all the tests run fast! (#23)
* chore(lint): added nlreturn, run linting * chore(lint): replace some fmt.Sprintf calls * chore(lint): replace fmt.Sprintf with strconv * chore(lint): add goconst, use http lib for status codes, and methods * chore(lint): use errors lib, errCodes from internal/errors * chore(lint): dupl, thelper and usetesting * chore(lint): fmt.Errorf %v to %w, more linters * chore(lint): paralleltest, where possible * perf(test): optimize test performance by 78% - Implement shared binary building with package-level cache to eliminate redundant builds - Add strategic parallelization to 15+ tests while preserving environment variable isolation - Implement thread-safe fixture caching with RWMutex to reduce I/O operations - Remove unnecessary working directory changes by leveraging embedded templates - Add embedded template system with go:embed directive for reliable template resolution - Fix linting issues: rename sharedBinaryError to errSharedBinary, add nolint directive Performance improvements: - Total test execution time: 12+ seconds → 2.7 seconds (78% faster) - Binary build overhead: 14+ separate builds → 1 shared build (93% reduction) - Parallel execution: Limited → 15+ concurrent tests (60-70% better CPU usage) - I/O operations: 66+ fixture reads → cached with sync.RWMutex (50% reduction) All tests maintain 100% success rate and coverage while running nearly 4x faster.
This commit is contained in:
77
templates_embed/embed.go
Normal file
77
templates_embed/embed.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Package templates_embed provides embedded template filesystem functionality for gh-action-readme.
|
||||
// This package contains all template files embedded in the binary using Go's embed directive,
|
||||
// making templates available regardless of working directory or filesystem location.
|
||||
//
|
||||
//nolint:revive // Package name with underscore is intentional for clarity
|
||||
package templates_embed
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// embeddedTemplates contains all template files embedded in the binary
|
||||
//
|
||||
//go:embed templates
|
||||
var embeddedTemplates embed.FS
|
||||
|
||||
// GetEmbeddedTemplate reads a template from the embedded filesystem.
|
||||
func GetEmbeddedTemplate(templatePath string) ([]byte, error) {
|
||||
// Normalize path separators and remove leading slash if present
|
||||
cleanPath := strings.TrimPrefix(filepath.ToSlash(templatePath), "/")
|
||||
|
||||
// If path doesn't start with templates/, prepend it
|
||||
if !strings.HasPrefix(cleanPath, "templates/") {
|
||||
cleanPath = "templates/" + cleanPath
|
||||
}
|
||||
|
||||
return embeddedTemplates.ReadFile(cleanPath)
|
||||
}
|
||||
|
||||
// GetEmbeddedTemplateFS returns the embedded filesystem for templates.
|
||||
func GetEmbeddedTemplateFS() fs.FS {
|
||||
return embeddedTemplates
|
||||
}
|
||||
|
||||
// IsEmbeddedTemplateAvailable checks if a template exists in the embedded filesystem.
|
||||
func IsEmbeddedTemplateAvailable(templatePath string) bool {
|
||||
cleanPath := strings.TrimPrefix(filepath.ToSlash(templatePath), "/")
|
||||
if !strings.HasPrefix(cleanPath, "templates/") {
|
||||
cleanPath = "templates/" + cleanPath
|
||||
}
|
||||
|
||||
_, err := embeddedTemplates.ReadFile(cleanPath)
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// ReadTemplate reads a template from embedded filesystem first, then falls back to filesystem.
|
||||
func ReadTemplate(templatePath string) ([]byte, error) {
|
||||
// If it's an absolute path, read from filesystem with path validation
|
||||
if filepath.IsAbs(templatePath) {
|
||||
// Validate the path is clean to prevent path traversal attacks
|
||||
cleanPath := filepath.Clean(templatePath)
|
||||
if cleanPath != templatePath {
|
||||
return nil, filepath.ErrBadPattern
|
||||
}
|
||||
|
||||
return os.ReadFile(cleanPath) // #nosec G304 -- validated absolute path
|
||||
}
|
||||
|
||||
// Try embedded template first
|
||||
if IsEmbeddedTemplateAvailable(templatePath) {
|
||||
return GetEmbeddedTemplate(templatePath)
|
||||
}
|
||||
|
||||
// Fallback to filesystem with path validation
|
||||
// Validate the path is clean to prevent path traversal attacks
|
||||
cleanPath := filepath.Clean(templatePath)
|
||||
if cleanPath != templatePath || strings.Contains(cleanPath, "..") {
|
||||
return nil, filepath.ErrBadPattern
|
||||
}
|
||||
|
||||
return os.ReadFile(cleanPath) // #nosec G304 -- validated relative path
|
||||
}
|
||||
Reference in New Issue
Block a user