Skip to content

Commit fd83b32

Browse files
SchumacherFMvdobler
authored andcommitted
ht: Checks if PhantomJS is installed
This prevents executing tests if phantomjs is not installed. It checks the PATH environment variable and caches the result.
1 parent fd55aa8 commit fd83b32

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

ht/phantomjs.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,3 +821,34 @@ func calibratePhantomjsOverhead() {
821821

822822
os.Remove(name)
823823
}
824+
825+
// isPhantomJSInstalled has three states: 0 not checked, 1 not available,
826+
// 2 available
827+
var isPhantomJSInstalled int
828+
829+
// IsPhantomJSInstalled returns true if PhantomJS has been installed. It caches
830+
// the result.
831+
func IsPhantomJSInstalled() bool {
832+
if isPhantomJSInstalled > 0 {
833+
return isPhantomJSInstalled == 2
834+
}
835+
if PhantomJSExecutable != "phantomjs" {
836+
return true
837+
}
838+
839+
paths := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
840+
isPhantomJSInstalled = 1
841+
for _, path := range paths {
842+
p := fmt.Sprintf("%s%s%s", path, string(os.PathSeparator), PhantomJSExecutable)
843+
if fileExists(p) {
844+
isPhantomJSInstalled = 2
845+
break
846+
}
847+
}
848+
return isPhantomJSInstalled == 2
849+
}
850+
851+
func fileExists(path string) bool {
852+
fi, err := os.Stat(path)
853+
return !os.IsNotExist(err) && fi.Size() > 0
854+
}

ht/phantomjs_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ var passingScreenshotTests = []*Test{
325325
}
326326

327327
func TestScreenshotPass(t *testing.T) {
328+
if !IsPhantomJSInstalled() {
329+
t.Skip("PhantomJS is not installed")
330+
}
331+
328332
ts := httptest.NewServer(http.HandlerFunc(screenshotHandler))
329333
defer ts.Close()
330334

@@ -406,6 +410,10 @@ var failingScreenshotTests = []*Test{
406410
}
407411

408412
func TestScreenshotFail(t *testing.T) {
413+
if !IsPhantomJSInstalled() {
414+
t.Skip("PhantomJS is not installed")
415+
}
416+
409417
ts := httptest.NewServer(http.HandlerFunc(screenshotHandler))
410418
defer ts.Close()
411419

@@ -484,6 +492,10 @@ var passingRenderedHTMLTests = []*Test{
484492
}
485493

486494
func TestRenderedHTMLPassing(t *testing.T) {
495+
if !IsPhantomJSInstalled() {
496+
t.Skip("PhantomJS is not installed")
497+
}
498+
487499
ts := httptest.NewServer(http.HandlerFunc(screenshotHandler))
488500
defer ts.Close()
489501

@@ -533,6 +545,10 @@ var passingRenderingTimeTests = []*Test{
533545
}
534546

535547
func TestRenderingTime(t *testing.T) {
548+
if !IsPhantomJSInstalled() {
549+
t.Skip("PhantomJS is not installed")
550+
}
551+
536552
ts := httptest.NewServer(http.HandlerFunc(screenshotHandler))
537553
defer ts.Close()
538554

@@ -579,6 +595,10 @@ var passingRenderingTimeTests2 = []*Test{
579595
}
580596

581597
func TestRenderingTime2(t *testing.T) {
598+
if !IsPhantomJSInstalled() {
599+
t.Skip("PhantomJS is not installed")
600+
}
601+
582602
ts := httptest.NewServer(http.HandlerFunc(animationHandler))
583603
defer ts.Close()
584604

@@ -669,6 +689,10 @@ func animationHandler(w http.ResponseWriter, r *http.Request) {
669689
}
670690

671691
func TestFancyScreenshotPass(t *testing.T) {
692+
if !IsPhantomJSInstalled() {
693+
t.Skip("PhantomJS is not installed")
694+
}
695+
672696
ts := httptest.NewServer(http.HandlerFunc(animationHandler))
673697
defer ts.Close()
674698

@@ -699,6 +723,10 @@ func TestFancyScreenshotPass(t *testing.T) {
699723
}
700724

701725
func TestFancyScreenshotFail(t *testing.T) {
726+
if !IsPhantomJSInstalled() {
727+
t.Skip("PhantomJS is not installed")
728+
}
729+
702730
ts := httptest.NewServer(http.HandlerFunc(animationHandler))
703731
defer ts.Close()
704732

0 commit comments

Comments
 (0)