2011-06-24 20:02:51 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package osutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Creates a file with the content "test" at path
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func createTestInclude(path string) error {
|
2011-06-24 20:02:51 +00:00
|
|
|
// Create a config file for OpenCamliInclude to play with
|
|
|
|
cf, e := os.Create(path)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cf, "test")
|
|
|
|
return cf.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calls OpenCamliInclude to open path, and checks that it containts "test"
|
|
|
|
func checkOpen(t *testing.T, path string) {
|
|
|
|
found, e := FindCamliInclude(path)
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("Failed to find %v", path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var file *os.File
|
|
|
|
file, e = os.Open(found)
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("Failed to open %v", path)
|
|
|
|
} else {
|
|
|
|
var d [10]byte
|
|
|
|
if n, _ := file.Read(d[:]); n != 4 {
|
|
|
|
t.Errorf("Read incorrect number of chars from test.config, wrong file?")
|
|
|
|
}
|
|
|
|
if string(d[0:4]) != "test" {
|
|
|
|
t.Errorf("Wrong test file content: %v", string(d[0:4]))
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for error when file doesn't exist
|
|
|
|
func TestOpenCamliIncludeNoFile(t *testing.T) {
|
|
|
|
// Test that error occurs if no such file
|
|
|
|
const notExist = "this_config_doesnt_exist.config"
|
|
|
|
_, e := FindCamliInclude(notExist)
|
|
|
|
if e == nil {
|
|
|
|
t.Errorf("Successfully opened config which doesn't exist: %v", notExist)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for when a file exists in CWD
|
|
|
|
func TestOpenCamliIncludeCWD(t *testing.T) {
|
|
|
|
const path string = "TestOpenCamliIncludeCWD.config"
|
|
|
|
if e := createTestInclude(path); e != nil {
|
|
|
|
t.Errorf("Couldn't create test config file, aborting test: %v", e)
|
|
|
|
return
|
|
|
|
}
|
2011-07-01 21:36:48 +00:00
|
|
|
defer os.Remove(path)
|
2011-06-24 20:02:51 +00:00
|
|
|
|
|
|
|
checkOpen(t, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for when a file exists in CAMLI_CONFIG_DIR
|
|
|
|
func TestOpenCamliIncludeDir(t *testing.T) {
|
|
|
|
const name string = "TestOpenCamliIncludeDir.config"
|
|
|
|
if e := createTestInclude("/tmp/" + name); e != nil {
|
|
|
|
t.Errorf("Couldn't create test config file, aborting test: %v", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove("/tmp/" + name)
|
|
|
|
os.Setenv("CAMLI_CONFIG_DIR", "/tmp")
|
|
|
|
defer os.Setenv("CAMLI_CONFIG_DIR", "")
|
|
|
|
|
|
|
|
checkOpen(t, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for when a file exits in CAMLI_INCLUDE_PATH
|
|
|
|
func TestOpenCamliIncludePath(t *testing.T) {
|
|
|
|
const name string = "TestOpenCamliIncludePath.config"
|
|
|
|
if e := createTestInclude("/tmp/" + name); e != nil {
|
|
|
|
t.Errorf("Couldn't create test config file, aborting test: %v", e)
|
|
|
|
return
|
|
|
|
}
|
2011-07-01 21:36:48 +00:00
|
|
|
defer os.Remove("/tmp/" + name)
|
2011-06-24 20:02:51 +00:00
|
|
|
defer os.Setenv("CAMLI_INCLUDE_PATH", "")
|
|
|
|
|
|
|
|
os.Setenv("CAMLI_INCLUDE_PATH", "/tmp")
|
|
|
|
checkOpen(t, name)
|
|
|
|
|
|
|
|
os.Setenv("CAMLI_INCLUDE_PATH", "/not/a/camli/config/dir:/tmp")
|
|
|
|
checkOpen(t, name)
|
|
|
|
|
|
|
|
os.Setenv("CAMLI_INCLUDE_PATH", "/not/a/camli/config/dir:/tmp:/another/fake/camli/dir")
|
|
|
|
checkOpen(t, name)
|
|
|
|
}
|