Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7c8cee1
Enhance CI workflow for Windows: add artifact compression and release…
ntatschner Dec 7, 2024
d15bec5
Refactor profile test: remove unnecessary import and update LoadProfi…
ntatschner Dec 7, 2024
d571b3e
Remove unused itemdelegate and shells test files
ntatschner Dec 7, 2024
cecc7d2
removing unneeded exe
ntatschner Dec 7, 2024
aceefac
Merge remote-tracking branch 'origin/main' into dev/refactoring-tests
ntatschner Dec 7, 2024
526aada
Update build command in GitHub Actions workflow to remove unnecessary…
ntatschner Dec 7, 2024
9b0ed14
Change workflow runners from Windows to Ubuntu for test and release jobs
ntatschner Dec 7, 2024
f363225
Update GitHub Actions workflow to run Go tests on Windows and add lin…
ntatschner Dec 7, 2024
11fb1bd
Update release job dependencies in GitHub Actions workflow
ntatschner Dec 7, 2024
b2884f1
Refactor GitHub Actions workflow to improve formatting and ensure con…
ntatschner Dec 7, 2024
1f8970b
Update Super-Linter configuration to use environment variables for Gi…
ntatschner Dec 7, 2024
7ebd670
Update Super-Linter environment variable names for consistency
ntatschner Dec 7, 2024
5395f7b
Update upload-artifact action to version 4.4.3 in GitHub Actions work…
ntatschner Dec 7, 2024
9952410
Update build command in GitHub Actions workflow to remove unnecessary…
ntatschner Dec 7, 2024
966cb88
Change workflow runners from Windows to Ubuntu for test and release jobs
ntatschner Dec 7, 2024
81855df
Update GitHub Actions workflow to run Go tests on Windows and add lin…
ntatschner Dec 7, 2024
c4e74a7
Update release job dependencies in GitHub Actions workflow
ntatschner Dec 7, 2024
0d06429
Refactor GitHub Actions workflow to improve formatting and ensure con…
ntatschner Dec 7, 2024
2c940a4
Update Super-Linter configuration to use environment variables for Gi…
ntatschner Dec 7, 2024
0080dfe
Update Super-Linter environment variable names for consistency
ntatschner Dec 7, 2024
3f5d39c
Update upload-artifact action to version 4.4.3 in GitHub Actions work…
ntatschner Dec 7, 2024
34cd50b
Merge branch 'dev/refactoring-tests' of https://github.com/ntatschner…
ntatschner Dec 7, 2024
854adea
Set fetch-depth to 0 in GitHub Actions workflow for full history chec…
ntatschner Dec 7, 2024
cb9f846
Refactor LoadProfile function to remove hash validation dependency an…
ntatschner Dec 7, 2024
b46bb99
Add GO_GOLANGCI_LINT_CLI_LINT_MODE variable to GitHub Actions workflow
ntatschner Dec 7, 2024
1109e9e
Remove linter_tests job from GitHub Actions workflow to streamline CI…
ntatschner Dec 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: Go
on:
push:
branches: [ "main" ]
tags: [ "v*" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: windows-latest
steps:
Expand All @@ -19,7 +19,53 @@ jobs:
go-version: '1.23'

- name: Build
run: go build -v ./...
run: go build -v -o PowerShellProfileLauncher.exe

- name: Compress Artifact to Zip
run: Compress-Archive -Path .\PowerShellProfileLauncher.exe, .\profiles.csv.tmpl, .\config.json.tmpl -DestinationPath .\PowerShellProfileLauncher.zip

- name: Upload Build Artifact
uses: actions/upload-artifact@v4.4.3
with:
name: PowerShellProfileLauncher
path: PowerShellProfileLauncher.zip

go_test_files:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Test
run: go test -v ./...

release:
runs-on: ubuntu-latest
needs: [build, go_test_files]
if: startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v4

- name: Download Build Artifact
uses: actions/download-artifact@v3
with:
name: PowerShellProfileLauncher

- id: create_release
name: Create Release
uses: ncipollo/release-action@v1.14.0
with:
name: ${{ github.ref_name }}
token: ${{ secrets.RELEASER }}
artifactErrorsFailBuild: true
discussionCategory: 'Releases'
generateReleaseNotes: true
makeLatest: true
skipIfReleaseExists: true
tag: ${{ github.ref_name }}
body: ${{ github.event.release.body }}
artifacts: PowerShellProfileLauncher.zip
Binary file removed GoPowerShellLauncher.zip
Binary file not shown.
10 changes: 4 additions & 6 deletions cmd/tests/config_test.go → cmd/utils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package utils
import (
"os"
"testing"

"github.com/ntatschner/GoPowerShellLauncher/cmd/utils"
)

func createTempConfigFile(content string) (string, error) {
Expand All @@ -25,13 +23,13 @@ func TestLoadConfig(t *testing.T) {
tests := []struct {
name string
fileContent string
expected *utils.Config
expected *Config
expectError bool
}{
{
name: "Valid config",
fileContent: `{"csv_path": "/path/to/csv"}`,
expected: &utils.Config{CsvPath: "/path/to/csv"},
expected: &Config{CsvPath: "/path/to/csv"},
expectError: false,
},
{
Expand All @@ -43,7 +41,7 @@ func TestLoadConfig(t *testing.T) {
{
name: "Empty config",
fileContent: `{}`,
expected: &utils.Config{CsvPath: ""},
expected: &Config{CsvPath: ""},
expectError: false,
},
}
Expand All @@ -56,7 +54,7 @@ func TestLoadConfig(t *testing.T) {
}
defer os.Remove(filePath)

config, err := utils.LoadConfig(filePath)
config, err := LoadConfig(filePath)
if (err != nil) != tt.expectError {
t.Errorf("loadConfig() error = %v, expectError %v", err, tt.expectError)
return
Expand Down
38 changes: 0 additions & 38 deletions cmd/utils/itemdelegate.go

This file was deleted.

32 changes: 7 additions & 25 deletions cmd/tests/profile_test.go → cmd/utils/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

l "github.com/ntatschner/GoPowerShellLauncher/cmd/logger"
"github.com/ntatschner/GoPowerShellLauncher/cmd/types"
"github.com/ntatschner/GoPowerShellLauncher/cmd/utils"
)

func createTempFileWithContent(content string) (string, error) {
Expand Down Expand Up @@ -45,6 +44,12 @@ func (m MockHashValidator) ValidateHash(expectedHash, filePath string) (bool, er
func TestLoadProfile(t *testing.T) {
// Create temporary files with known content
_ = l.InitLogger("", "testslog.log", "debug")
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
if err != nil {
t.Fatalf("Failed to open os.DevNull: %v", err)
}
defer devNull.Close()
l.Logger.SetOutput(devNull)
file1Content := "content1"
file2Content := "content2"
file1Path, err := createTempFileWithContent(file1Content)
Expand Down Expand Up @@ -77,7 +82,6 @@ func TestLoadProfile(t *testing.T) {
Shell: "powershell",
ItemDescription: "A valid description",
IsValidPath: true,
IsValidHash: true,
IsValidShellVersion: true,
IsValidDescription: true,
},
Expand All @@ -91,21 +95,6 @@ func TestLoadProfile(t *testing.T) {
Shell: "powershell",
ItemDescription: "A valid description",
IsValidPath: false,
IsValidHash: true,
IsValidShellVersion: true,
IsValidDescription: true,
},
},
{
name: "Invalid hash",
line: []string{file1Path, "", "powershell", "A valid description"},
expected: types.ProfileItem{
Path: file1Path,
Hash: "",
Shell: "powershell",
ItemDescription: "A valid description",
IsValidPath: true,
IsValidHash: false,
IsValidShellVersion: true,
IsValidDescription: true,
},
Expand All @@ -119,7 +108,6 @@ func TestLoadProfile(t *testing.T) {
Shell: "invalidShell",
ItemDescription: "A valid description",
IsValidPath: true,
IsValidHash: true,
IsValidShellVersion: false,
IsValidDescription: true,
},
Expand All @@ -133,7 +121,6 @@ func TestLoadProfile(t *testing.T) {
Shell: "powershell",
ItemDescription: "A very long description that exceeds the maximum allowed length of 100 characters. This description should be considered invalid.",
IsValidPath: true,
IsValidHash: true,
IsValidShellVersion: true,
IsValidDescription: false,
},
Expand All @@ -147,7 +134,6 @@ func TestLoadProfile(t *testing.T) {
Shell: "",
ItemDescription: "",
IsValidPath: false,
IsValidHash: false,
IsValidShellVersion: false,
IsValidDescription: true,
},
Expand All @@ -161,7 +147,6 @@ func TestLoadProfile(t *testing.T) {
Shell: "bash",
ItemDescription: "Another valid description",
IsValidPath: true,
IsValidHash: true,
IsValidShellVersion: false,
IsValidDescription: true,
},
Expand All @@ -170,7 +155,7 @@ func TestLoadProfile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.LoadProfile(tt.line, MockHashValidator{})
result := LoadProfile(tt.line)
if result.Path != tt.expected.Path {
t.Errorf("LoadProfile().Path = %v, expected %v", result.Path, tt.expected.Path)
}
Expand All @@ -186,9 +171,6 @@ func TestLoadProfile(t *testing.T) {
if result.IsValidPath != tt.expected.IsValidPath {
t.Errorf("LoadProfile().IsValidPath = %v, expected %v", result.IsValidPath, tt.expected.IsValidPath)
}
if result.IsValidHash != tt.expected.IsValidHash {
t.Errorf("LoadProfile().IsValidHash = %v, expected %v", result.IsValidHash, tt.expected.IsValidHash)
}
if result.IsValidShellVersion != tt.expected.IsValidShellVersion {
t.Errorf("LoadProfile().IsValidShellVersion = %v, expected %v", result.IsValidShellVersion, tt.expected.IsValidShellVersion)
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/utils/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// line: a slice of strings containing the profile data.
// Returns:
// - ProfileItem: a ProfileItem struct with the loaded and validated data.
func LoadProfile(line []string, hashValidator HashValidator) types.ProfileItem {
func LoadProfile(line []string) types.ProfileItem {
l.Logger.Info("Loading profile", "line", line)
p := types.ProfileItem{
Path: line[0],
Expand All @@ -24,22 +24,22 @@ func LoadProfile(line []string, hashValidator HashValidator) types.ProfileItem {
ItemDescription: line[3],
}
p.ItemTitle = p.GetName()
isValidPath, patherr := validatePath(p.Path)
isValidPath, patherr := ValidatePath(p.Path)
if patherr != nil {
l.Logger.Error(fmt.Sprintf("Failed to validate path %s", p.Path), "error", patherr)
}
p.IsValidPath = isValidPath
isValidHash, hasherr := hashValidator.ValidateHash(p.Hash, p.Path)
isValidHash, hasherr := ValidateHash(p.Hash, p.Path)
if hasherr != nil {
l.Logger.Error(fmt.Sprintf("Failed to validate hash for path %s", p.Path), "error", hasherr)
}
p.IsValidHash = isValidHash
isValidShell, shellerr := validateShellVersion(p.Shell)
isValidShell, shellerr := ValidateShellVersion(p.Shell)
if shellerr != nil {
l.Logger.Error(fmt.Sprintf("Failed to validate shell version %s", p.Shell), "error", shellerr)
}
p.IsValidShellVersion = isValidShell
isValidDescription, descerr := validateDescription(p.ItemDescription)
isValidDescription, descerr := ValidateDescription(p.ItemDescription)
if descerr != nil {
l.Logger.Error(fmt.Sprintf("Failed to validate description %s", p.ItemDescription), "error", descerr)
}
Expand Down Expand Up @@ -85,13 +85,12 @@ func LoadProfiles(filePath string) ([]types.ProfileItem, error) {

l.Logger.Info(fmt.Sprintf("Loaded %d records from CSV file", len(records)-1))
var profiles []types.ProfileItem
var hashValidator HashValidator
for i, record := range records[1:] {
if len(record) != len(expectedHeaders) {
l.Logger.Error("Wrong number of fields", "line", i+2, "record", record)
continue
}
profile := LoadProfile(record, hashValidator)
profile := LoadProfile(record)
l.Logger.Info(fmt.Sprintf("Processing profile: %+v", profile))
profiles = append(profiles, profile)
l.Logger.Info(fmt.Sprintf("Added profile: %+v", profile))
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/shells.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func LoadShells() ([]types.ShellItem, error) {
}
for s := range items {
l.Logger.Info("Processing shell", "shell", items[s])
_, err := validatePath(items[s].Path)
_, err := ValidatePath(items[s].Path)
if err != nil {
l.Logger.Warn("Invalid shell path", "Error", err)
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/utils/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type HashValidator interface {
type DefaultHashValidator struct{}

func (d DefaultHashValidator) ValidateHash(expectedHash, filePath string) (bool, error) {
return validateHash(expectedHash, filePath)
return ValidateHash(expectedHash, filePath)
}

func validatePath(path string) (bool, error) {
func ValidatePath(path string) (bool, error) {
l.Logger.Info("Validating path", "Path", path)
_, err := os.Stat(path)
if os.IsNotExist(err) {
Expand All @@ -37,7 +37,7 @@ func validatePath(path string) (bool, error) {
return true, nil
}

func compareHashes(hash1, hash2 []byte) (bool, error) {
func CompareHashes(hash1, hash2 []byte) (bool, error) {
l.Logger.Info("Comparing hashes", "Hash1", hex.EncodeToString(hash1), "Hash2", hex.EncodeToString(hash2))
if len(hash1) != len(hash2) {
return false, fmt.Errorf("hashes have different lengths")
Expand All @@ -51,7 +51,7 @@ func compareHashes(hash1, hash2 []byte) (bool, error) {
return true, nil
}

func validateHash(expectedHash, filePath string) (bool, error) {
func ValidateHash(expectedHash, filePath string) (bool, error) {
l.Logger.Info("Validating hash", "ExpectedHash", expectedHash, "FilePath", filePath)

// Decode the expected hash from hex
Expand All @@ -75,15 +75,15 @@ func validateHash(expectedHash, filePath string) (bool, error) {
computedHash := hasher.Sum(nil)

// Compare the computed hash with the expected hash
_, err = compareHashes(expectedHashBytes, computedHash)
_, err = CompareHashes(expectedHashBytes, computedHash)
if err != nil {
return false, fmt.Errorf("hash mismatch: expected %x, got %x", expectedHashBytes, computedHash)
}

return true, nil
}

func validateShellVersion(shellVersion string) (bool, error) {
func ValidateShellVersion(shellVersion string) (bool, error) {
l.Logger.Info("Validating shell version", "ShellVersion", shellVersion)
shellVersion = strings.ToLower(shellVersion)
switch shellVersion {
Expand All @@ -94,7 +94,7 @@ func validateShellVersion(shellVersion string) (bool, error) {
return false, fmt.Errorf("invalid shell version: %s", shellVersion)
}

func validateDescription(description string) (bool, error) {
func ValidateDescription(description string) (bool, error) {
l.Logger.Info("Validating description", "Description", description)
if len(description) > 100 {
return false, fmt.Errorf("description is too long (max 100 characters)")
Expand Down