Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed May 14, 2024
1 parent d612022 commit 347277d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 30 deletions.
60 changes: 30 additions & 30 deletions lib/detectors/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,66 @@ import "regexp"

// An OS represents a target operating system.
type OS struct {
name string
regex *regexp.Regexp
anti *regexp.Regexp
priority *regexp.Regexp // matches to priority are better than normal matches
Name string
Regex *regexp.Regexp
Anti *regexp.Regexp
Priority *regexp.Regexp // matches to priority are better than normal matches
}

// Match returns true if the given archive name is likely to store a binary for
// this OS. Also returns if this is a priority match.
func (os *OS) Match(s string) (bool, bool) {
if os.anti != nil && os.anti.MatchString(s) {
if os.Anti != nil && os.Anti.MatchString(s) {
return false, false
}
if os.priority != nil {
return os.regex.MatchString(s), os.priority.MatchString(s)
if os.Priority != nil {
return os.Regex.MatchString(s), os.Priority.MatchString(s)
}
return os.regex.MatchString(s), false
return os.Regex.MatchString(s), false
}

var (
OSDarwin = OS{
name: "darwin",
regex: regexp.MustCompile(`(?i)(darwin|mac.?(os)?|osx)`),
Name: "darwin",
Regex: regexp.MustCompile(`(?i)(darwin|mac.?(os)?|osx)`),
}
OSWindows = OS{
name: "windows",
regex: regexp.MustCompile(`(?i)([^r]win|windows)`),
Name: "windows",
Regex: regexp.MustCompile(`(?i)([^r]win|windows)`),
}
OSLinux = OS{
name: "linux",
regex: regexp.MustCompile(`(?i)(linux|ubuntu)`),
anti: regexp.MustCompile(`(?i)(android)`),
priority: regexp.MustCompile(`\.appimage$`),
Name: "linux",
Regex: regexp.MustCompile(`(?i)(linux|ubuntu)`),
Anti: regexp.MustCompile(`(?i)(android)`),
Priority: regexp.MustCompile(`\.appimage$`),
}
OSNetBSD = OS{
name: "netbsd",
regex: regexp.MustCompile(`(?i)(netbsd)`),
Name: "netbsd",
Regex: regexp.MustCompile(`(?i)(netbsd)`),
}
OSFreeBSD = OS{
name: "freebsd",
regex: regexp.MustCompile(`(?i)(freebsd)`),
Name: "freebsd",
Regex: regexp.MustCompile(`(?i)(freebsd)`),
}
OSOpenBSD = OS{
name: "openbsd",
regex: regexp.MustCompile(`(?i)(openbsd)`),
Name: "openbsd",
Regex: regexp.MustCompile(`(?i)(openbsd)`),
}
OSAndroid = OS{
name: "android",
regex: regexp.MustCompile(`(?i)(android)`),
Name: "android",
Regex: regexp.MustCompile(`(?i)(android)`),
}
OSIllumos = OS{
name: "illumos",
regex: regexp.MustCompile(`(?i)(illumos)`),
Name: "illumos",
Regex: regexp.MustCompile(`(?i)(illumos)`),
}
OSSolaris = OS{
name: "solaris",
regex: regexp.MustCompile(`(?i)(solaris)`),
Name: "solaris",
Regex: regexp.MustCompile(`(?i)(solaris)`),
}
OSPlan9 = OS{
name: "plan9",
regex: regexp.MustCompile(`(?i)(plan9)`),
Name: "plan9",
Regex: regexp.MustCompile(`(?i)(plan9)`),
}
)

Expand Down
57 changes: 57 additions & 0 deletions lib/detectors/os_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package detectors_test

import (
"regexp"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/permafrost-dev/eget/lib/detectors"
)

var _ = Describe("OS", func() {
var (
osDarwin OS
osWindows OS
osLinux OS
)

BeforeEach(func() {
osDarwin = OS{
Name: "darwin",
Regex: regexp.MustCompile(`(?i)(darwin|mac.?(os)?|osx)`),
}
osWindows = OS{
Name: "windows",
Regex: regexp.MustCompile(`(?i)([^r]win|windows)`),
}
osLinux = OS{
Name: "linux",
Regex: regexp.MustCompile(`(?i)(linux|ubuntu)`),
Anti: regexp.MustCompile(`(?i)(android)`),
Priority: regexp.MustCompile(`\.appimage$`),
}
})

Describe("Match", func() {
Context("with an OS that matches the given string", func() {
It("should return true for a matching OS string", func() {
Expect(osDarwin.Match("macOS")).To(Equal(true))
Expect(osWindows.Match("windows10")).To(Equal(true))
})

It("should return false for a non-matching OS string", func() {
Expect(osDarwin.Match("linux")).To(BeFalse())
Expect(osWindows.Match("darwin")).To(BeFalse())
})

It("should respect anti-patterns", func() {
Expect(osLinux.Match("android")).To(BeFalse())
})

It("should detect priority matches", func() {
_, priority := osLinux.Match("app.appimage")
Expect(priority).To(BeTrue())
})
})
})
})

0 comments on commit 347277d

Please sign in to comment.