Commit 46636b16 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb/fs1: tests: Factor-out checking for Python/ZODB presence into common place

We will soon need to check for Python and other modules in more tests.
Now the checking function is generic - see xtesting.NeedPy docs.
parent 2fc04a08
// internal/ is a staging place for common utilities not yet sorted out
// to their proper homes.
// Copyright (C) 2017-2019 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package xtesting provides addons to std package testing.
package xtesting
import (
"os/exec"
"sync"
"testing"
)
var (
pyMu sync.Mutex
pyHave = map[string]bool{} // {} pymod -> y/n ; ".python" indicates python presence
)
// NeedPy skips current test if python and specified modules are not available.
//
// For example
//
// xtesting.NeedPy(t)
//
// would check for python presence, and
//
// xtesting.NeedPy(t, "ZODB", "golang.strconv")
//
// would check if all python and ZODB and golang.strconv python modules are available.
func NeedPy(t testing.TB, modules ...string) {
pyMu.Lock()
defer pyMu.Unlock()
// verify if python is present
havePy, know := pyHave[".python"]
if !know {
cmd := exec.Command("python2", "-c", "0")
err := cmd.Run()
havePy = (err == nil)
pyHave[".python"] = havePy
}
if !havePy {
t.Skipf("skipping: python is not availble")
}
var donthave []string
for _, pymod := range modules {
have, know := pyHave[pymod]
if !know {
cmd := exec.Command("python2", "-c", "import "+pymod)
err := cmd.Run()
have = (err == nil)
pyHave[pymod] = have
}
if !have {
donthave = append(donthave, pymod)
}
}
if len(donthave) != 0 {
t.Skipf("skipping: the following python modules are not available: %s", donthave)
}
// we verified everything - now it is ok not to skip.
return
}
// Copyright (C) 2017 Nexedi SA and Contributors. // Copyright (C) 2017-2019 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your // it under the terms of the GNU General Public License version 3, or (at your
...@@ -32,6 +32,7 @@ import ( ...@@ -32,6 +32,7 @@ import (
"strings" "strings"
"testing" "testing"
"lab.nexedi.com/kirr/neo/go/internal/xtesting"
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1/fsb" "lab.nexedi.com/kirr/neo/go/zodb/storage/fs1/fsb"
) )
...@@ -201,7 +202,7 @@ func TestIndexLoadFromPy(t *testing.T) { ...@@ -201,7 +202,7 @@ func TestIndexLoadFromPy(t *testing.T) {
// test zodb/py can read index data as saved by us // test zodb/py can read index data as saved by us
func TestIndexSaveToPy(t *testing.T) { func TestIndexSaveToPy(t *testing.T) {
needZODBPy(t) xtesting.NeedPy(t, "ZODB")
workdir := xworkdir(t) workdir := xworkdir(t)
err := _1fs_index.SaveFile(workdir + "/1.fs.index") err := _1fs_index.SaveFile(workdir + "/1.fs.index")
...@@ -289,19 +290,11 @@ func BenchmarkIndexGet(b *testing.B) { ...@@ -289,19 +290,11 @@ func BenchmarkIndexGet(b *testing.B) {
} }
} }
var haveZODBPy = false
var workRoot string var workRoot string
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// check whether we have zodb/py
cmd := exec.Command("python2", "-c", "import ZODB")
err := cmd.Run()
if err == nil {
haveZODBPy = true
}
// setup work root for all tests // setup work root for all tests
workRoot, err = ioutil.TempDir("", "t-index") workRoot, err := ioutil.TempDir("", "t-index")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -313,13 +306,6 @@ func TestMain(m *testing.M) { ...@@ -313,13 +306,6 @@ func TestMain(m *testing.M) {
os.Exit(exit) os.Exit(exit)
} }
func needZODBPy(t *testing.T) {
if haveZODBPy {
return
}
t.Skipf("skipping: zodb/py is not available")
}
// create temp dir inside workRoot // create temp dir inside workRoot
func xworkdir(t testing.TB) string { func xworkdir(t testing.TB) string {
work, err := ioutil.TempDir(workRoot, "") work, err := ioutil.TempDir(workRoot, "")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment