Skip to content

Commit

Permalink
Fix/remove go version dependency (#36)
Browse files Browse the repository at this point in the history
* Remove hashicorp/go-version dependency
* Adds Fossa analysis to the actions workflow
* Fixes warning related to the actions/setup-go step
* Add tests for VersionInfo methods and simplify comparisons
  • Loading branch information
matzefriedrich authored Oct 8, 2024
1 parent c217f05 commit 6a02fa7
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 14 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Go environment
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '1.23'

Expand All @@ -27,4 +27,18 @@ jobs:
- name: Send coverage report
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=covprofile -service=github
run: goveralls -coverprofile=covprofile -service=github

code-security-analysis:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Install Fossa
run: |
curl -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/fossas/fossa-cli/master/install-latest.sh | bash
- name: Run Code Security Analysis
env:
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}
run: fossa analyze
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v1.0.3] - 2024-10-08

### Changed

* Removed `hashicorp/go-version` dependency; added simple comparison functions to the `version.go` module instead.


## [v1.0.2] - 2024-09-26

### Changed

* Updated package dependencies (as reported by Dependabot).


## [v1.0.1] - 2024-09-23

### Added
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/matzefriedrich/parsley)
![GitHub Release](https://img.shields.io/github/v/release/matzefriedrich/parsley?include_prereleases)

[![FOSSA Status](https://app.fossa.com/api/projects/custom%2B48327%2Fgithub.com%2Fmatzefriedrich%2Fparsley.svg?type=shield&issueType=license)](https://app.fossa.com/projects/custom%2B48327%2Fgithub.com%2Fmatzefriedrich%2Fparsley?ref=badge_shield&issueType=license)
[![FOSSA Status](https://app.fossa.com/api/projects/custom%2B48327%2Fgithub.com%2Fmatzefriedrich%2Fparsley.svg?type=shield&issueType=security)](https://app.fossa.com/projects/custom%2B48327%2Fgithub.com%2Fmatzefriedrich%2Fparsley?ref=badge_shield&issueType=security)

## What is Parsley?

Parsley is a powerful, easy-to-use reflection-based dependency injection package that integrates seamlessly into any Go application.
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/matzefriedrich/parsley
go 1.23

require (
github.com/hashicorp/go-version v1.7.0
github.com/matzefriedrich/cobra-extensions v0.3.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/matzefriedrich/cobra-extensions v0.3.1 h1:yNeDzlaI/6DvzXj7CO/ZJmibK0V011tVc7PJQgYfteQ=
Expand Down
55 changes: 46 additions & 9 deletions internal/utils/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"fmt"
"regexp"
"strconv"

"github.com/hashicorp/go-version"
)

const (
VersionString string = "1.0.1"
VersionString string = "1.0.3"
)

// VersionInfo represents the version details using semantic versioning.
Expand All @@ -20,23 +18,52 @@ type VersionInfo struct {
Patch int
}

// ComparisonResult represents the result of comparing two values.
type ComparisonResult int

const (

// LessThan indicates that the first value is less than the second in a comparison operation.
LessThan ComparisonResult = iota

// Equal indicates that the first value is equal to the second in a comparison operation.
Equal

// GreaterThan indicates that the first value is greater than the second in a comparison operation.
GreaterThan
)

// String returns the VersionInfo as a formatted string in the semantic versioning format (Major.Minor.Patch).
func (v VersionInfo) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}

// LessThan determines if the current VersionInfo is less than the specified VersionInfo based on semantic versioning.
func (v VersionInfo) LessThan(other VersionInfo) bool {
a, _ := version.NewVersion(v.String())
b, _ := version.NewVersion(other.String())
return a.LessThan(b)
return v.Compare(other) == LessThan
}

// LessThanOrEqual determines if the current VersionInfo is less than or equal to the specified VersionInfo based on semantic versioning.
func (v VersionInfo) LessThanOrEqual(other VersionInfo) bool {
result := v.Compare(other)
return result == Equal || result == LessThan
}

// Equal determines if two VersionInfo instances represent the same version based on semantic versioning.
func (v VersionInfo) Equal(other VersionInfo) bool {
a, _ := version.NewVersion(v.String())
b, _ := version.NewVersion(other.String())
return a.Equal(b)
return v.Compare(other) == Equal
}

// Compare compares the current VersionInfo instance with another VersionInfo instance and returns a ComparisonResult.
func (v VersionInfo) Compare(other VersionInfo) ComparisonResult {

if isLessThan(v, other) {
return LessThan
} else if isEqual(v, other) {
return Equal
}

return GreaterThan
}

// ApplicationVersion parses and returns the application's version information. If the version is not set, an error is returned.
Expand All @@ -48,6 +75,16 @@ func ApplicationVersion() (*VersionInfo, error) {
return v, nil
}

func isLessThan(v VersionInfo, other VersionInfo) bool {
return v.Major < other.Major ||
(v.Major == other.Major && v.Minor < other.Minor) ||
(v.Major == other.Major && v.Minor == other.Minor && v.Patch < other.Patch)
}

func isEqual(v VersionInfo, other VersionInfo) bool {
return v.Major == other.Major && v.Minor == other.Minor && v.Patch == other.Patch
}

func tryParseVersionInfo(version string) (*VersionInfo, error) {

re := regexp.MustCompile("(?:[vV])?(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)")
Expand Down
99 changes: 99 additions & 0 deletions internal/utils/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package utils

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_VersionInfo_tryParseVersionInfo_valid_semantic_version_expression_with_prefix_does_not_return_err(t *testing.T) {

// Arrange
const version = "v0.1.0"

// Act
actual, err := tryParseVersionInfo(version)

// Assert
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, "0.1.0", actual.String())
}

func Test_VersionInfo_tryParseVersionInfo_valid_semantic_version_expression_no_prefix_does_not_return_err(t *testing.T) {

// Arrange
const version = "0.1.0"

// Act
actual, err := tryParseVersionInfo(version)

// Assert
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, "0.1.0", actual.String())
}

func Test_VersionInfo_tryParseVersionInfo_invalid_semantic_version_expression_returns_err(t *testing.T) {

// Arrange
const version = "a.b.c"

// Act
actual, err := tryParseVersionInfo(version)

// Assert
assert.Error(t, err)
assert.Nil(t, actual)
}

func Test_VersionInfo_LessThan(t *testing.T) {

// Arrange
const a = "v0.1.0"
const b = "v0.2.0"

version, _ := tryParseVersionInfo(a)
otherVersion, _ := tryParseVersionInfo(b)

// Act
actual := version.LessThan(*otherVersion)

// Assert
assert.True(t, actual)
}

func Test_VersionInfo_LessThanOrEqual(t *testing.T) {

// Arrange
const a = "v0.1.0"
b := []string{"v0.2.0", "v0.1.0"}

version, _ := tryParseVersionInfo(a)

// Act
for _, v := range b {
otherVersion, _ := tryParseVersionInfo(v)

actual := version.LessThanOrEqual(*otherVersion)

// Assert
assert.True(t, actual)
}
}

func Test_VersionInfo_Equal(t *testing.T) {

// Arrange
const a = "v0.1.0"
const b = "v0.1.0"

version, _ := tryParseVersionInfo(a)
otherVersion, _ := tryParseVersionInfo(b)

// Act

actual := version.Equal(*otherVersion)

// Assert
assert.True(t, actual)
}

0 comments on commit 6a02fa7

Please sign in to comment.