Skip to content

Commit e76629f

Browse files
committed
refacto
1 parent b74dc4c commit e76629f

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

store.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,24 @@ type PHPStore struct {
4444

4545
// New creates a new PHP store
4646
func New(configDir string, reload bool, logger func(msg string, a ...interface{})) *PHPStore {
47-
s := &PHPStore{
48-
configDir: configDir,
49-
seen: make(map[string]int),
50-
discoveryLogFunc: logger,
51-
}
47+
s := newEmpty(configDir, logger)
48+
5249
if reload {
5350
_ = os.Remove(filepath.Join(configDir, "php_versions.json"))
5451
}
5552
s.loadVersions()
5653
return s
5754
}
5855

56+
// newEmpty creates a new "empty" (without loading versions) PHP store
57+
func newEmpty(configDir string, logger func(msg string, a ...interface{})) *PHPStore {
58+
return &PHPStore{
59+
configDir: configDir,
60+
seen: make(map[string]int),
61+
discoveryLogFunc: logger,
62+
}
63+
}
64+
5965
// Versions returns all available PHP versions
6066
func (s *PHPStore) Versions() []*Version {
6167
return s.versions

store_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
)
77

88
func TestBestVersion(t *testing.T) {
9-
store := New("/dev/null", false, nil)
9+
store := newEmpty("/dev/null", nil)
1010
for _, v := range []string{"7.4.33", "8.0.27", "8.1.2", "8.1.14", "8.2.1"} {
11-
store.addVersion(&Version{
12-
Version: v,
13-
PHPPath: filepath.Join("/foo", v, "bin", "php"),
14-
})
11+
ver := NewVersion(v)
12+
ver.PHPPath = filepath.Join("/foo", v, "bin", "php")
13+
store.addVersion(ver)
1514

1615
if !store.IsVersionAvailable(v) {
1716
t.Errorf("Version %s should be shown as available", v)
@@ -23,7 +22,7 @@ func TestBestVersion(t *testing.T) {
2322
if bestVersion == nil {
2423
t.Error("8 requirement should find a best version")
2524
} else if bestVersion.Version != "8.2.1" {
26-
t.Error("8 requirement should find 8.2.1 as best version")
25+
t.Errorf("8 requirement should find 8.2.1 as best version, got %s", bestVersion.Version)
2726
}
2827
}
2928

@@ -32,7 +31,7 @@ func TestBestVersion(t *testing.T) {
3231
if bestVersion == nil {
3332
t.Error("8.1 requirement should find a best version")
3433
} else if bestVersion.Version != "8.1.14" {
35-
t.Error("8.1 requirement should find 8.1.14 as best version")
34+
t.Errorf("8.1 requirement should find 8.1.14 as best version, got %s", bestVersion.Version)
3635
}
3736
}
3837

@@ -41,7 +40,7 @@ func TestBestVersion(t *testing.T) {
4140
if bestVersion == nil {
4241
t.Error("8.0.10 requirement should find a best version")
4342
} else if bestVersion.Version != "8.0.27" {
44-
t.Error("8.0.10 requirement should find 8.0.27 as best version")
43+
t.Errorf("8.0.10 requirement should find 8.0.27 as best version, got %s", bestVersion.Version)
4544
} else if warning == "" {
4645
t.Error("8.0.10 requirement should trigger a warning")
4746
}
@@ -52,7 +51,7 @@ func TestBestVersion(t *testing.T) {
5251
if bestVersion == nil {
5352
t.Error("8.0.99 requirement should find a best version")
5453
} else if bestVersion.Version != "8.0.27" {
55-
t.Error("8.0.99 requirement should find 8.0.27 as best version")
54+
t.Errorf("8.0.99 requirement should find 8.0.27 as best version, got %s", bestVersion.Version)
5655
} else if warning != "" {
5756
t.Error("8.0.99 requirement should not trigger a warning")
5857
}

version.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ type Version struct {
5151
FrankenPHP bool `json:"frankenphp"`
5252
}
5353

54+
func NewVersion(v string) *Version {
55+
return &Version{
56+
Version: v,
57+
FullVersion: version.Must(version.NewVersion(v)),
58+
}
59+
}
60+
5461
type versions []*Version
5562

5663
func (vs versions) Len() int { return len(vs) }

0 commit comments

Comments
 (0)