github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/dev/devcam/test.go (about)

     1  /*
     2  Copyright 2013 The Camlistore Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This file adds the "test" subcommand to devcam, to run the full test suite.
    18  
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	"os"
    25  	"os/exec"
    26  	"path/filepath"
    27  	"strings"
    28  
    29  	"camlistore.org/pkg/cmdmain"
    30  )
    31  
    32  type testCmd struct {
    33  	// start of flag vars
    34  	short bool
    35  	// end of flag vars
    36  
    37  	// buildGoPath becomes our child "go" processes' GOPATH environment variable
    38  	buildGoPath string
    39  }
    40  
    41  func init() {
    42  	cmdmain.RegisterCommand("test", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    43  		cmd := new(testCmd)
    44  		flags.BoolVar(&cmd.short, "short", false, "Use '-short' with go test.")
    45  		return cmd
    46  	})
    47  }
    48  
    49  func (c *testCmd) Usage() {
    50  	fmt.Fprintf(cmdmain.Stderr, "Usage: devcam test\n")
    51  }
    52  
    53  func (c *testCmd) Describe() string {
    54  	return "run the full test suite."
    55  }
    56  
    57  func (c *testCmd) RunCommand(args []string) error {
    58  	if err := c.syncSrc(); err != nil {
    59  		return err
    60  	}
    61  	buildSrcDir := filepath.Join(c.buildGoPath, "src", "camlistore.org")
    62  	if err := os.Chdir(buildSrcDir); err != nil {
    63  		return err
    64  	}
    65  	if err := c.buildSelf(); err != nil {
    66  		return err
    67  	}
    68  	if err := c.runTests(args); err != nil {
    69  		return err
    70  	}
    71  	println("PASS")
    72  	return nil
    73  }
    74  
    75  func (c *testCmd) env() *Env {
    76  	if c.buildGoPath == "" {
    77  		panic("called too early")
    78  	}
    79  	env := NewCopyEnv()
    80  	env.NoGo()
    81  	env.Set("GOPATH", c.buildGoPath)
    82  	return env
    83  }
    84  
    85  func (c *testCmd) syncSrc() error {
    86  	args := []string{"run", "make.go", "--onlysync"}
    87  	cmd := exec.Command("go", args...)
    88  	cmd.Stderr = os.Stderr
    89  	out, err := cmd.Output()
    90  	if err != nil {
    91  		return fmt.Errorf("Error populating tmp src tree: %v", err)
    92  	}
    93  	c.buildGoPath = strings.TrimSpace(string(out))
    94  	return nil
    95  }
    96  
    97  func (c *testCmd) buildSelf() error {
    98  	args := []string{
    99  		"install",
   100  		filepath.FromSlash("./dev/devcam"),
   101  	}
   102  	cmd := exec.Command("go", args...)
   103  	binDir, err := filepath.Abs("bin")
   104  	if err != nil {
   105  		return fmt.Errorf("Error setting GOBIN: %v", err)
   106  	}
   107  	env := c.env()
   108  	env.Set("GOBIN", binDir)
   109  	cmd.Env = env.Flat()
   110  	cmd.Stdout = os.Stdout
   111  	cmd.Stderr = os.Stderr
   112  	if err := cmd.Run(); err != nil {
   113  		return fmt.Errorf("Error building devcam: %v", err)
   114  	}
   115  	return nil
   116  }
   117  
   118  func (c *testCmd) runTests(args []string) error {
   119  	targs := []string{"test"}
   120  	if !strings.HasSuffix(c.buildGoPath, "-nosqlite") {
   121  		targs = append(targs, "--tags=with_sqlite")
   122  	}
   123  	if c.short {
   124  		targs = append(targs, "-short")
   125  	}
   126  	if len(args) > 0 {
   127  		targs = append(targs, args...)
   128  	} else {
   129  		targs = append(targs, []string{
   130  			"./pkg/...",
   131  			"./server/camlistored",
   132  			"./server/appengine",
   133  			"./cmd/...",
   134  		}...)
   135  	}
   136  	env := c.env()
   137  	env.Set("SKIP_DEP_TESTS", "1")
   138  	return runExec("go", targs, env)
   139  }