github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/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 len(args) != 0 {
    59  		c.Usage()
    60  	}
    61  	if err := c.syncSrc(); err != nil {
    62  		return err
    63  	}
    64  	buildSrcDir := filepath.Join(c.buildGoPath, "src", "camlistore.org")
    65  	if err := os.Chdir(buildSrcDir); err != nil {
    66  		return err
    67  	}
    68  	if err := c.buildSelf(); err != nil {
    69  		return err
    70  	}
    71  	if err := c.runTests(); err != nil {
    72  		return err
    73  	}
    74  	println("PASS")
    75  	return nil
    76  }
    77  
    78  func (c *testCmd) env() *Env {
    79  	if c.buildGoPath == "" {
    80  		panic("called too early")
    81  	}
    82  	env := NewCopyEnv()
    83  	env.NoGo()
    84  	env.Set("GOPATH", c.buildGoPath)
    85  	return env
    86  }
    87  
    88  func (c *testCmd) syncSrc() error {
    89  	args := []string{"run", "make.go", "--onlysync"}
    90  	cmd := exec.Command("go", args...)
    91  	cmd.Stderr = os.Stderr
    92  	out, err := cmd.Output()
    93  	if err != nil {
    94  		return fmt.Errorf("Error populating tmp src tree: %v", err)
    95  	}
    96  	c.buildGoPath = strings.TrimSpace(string(out))
    97  	return nil
    98  }
    99  
   100  func (c *testCmd) buildSelf() error {
   101  	args := []string{
   102  		"install",
   103  		filepath.FromSlash("./dev/devcam"),
   104  	}
   105  	cmd := exec.Command("go", args...)
   106  	binDir, err := filepath.Abs("bin")
   107  	if err != nil {
   108  		return fmt.Errorf("Error setting GOBIN: %v", err)
   109  	}
   110  	env := c.env()
   111  	env.Set("GOBIN", binDir)
   112  	cmd.Env = env.Flat()
   113  	cmd.Stdout = os.Stdout
   114  	cmd.Stderr = os.Stderr
   115  	if err := cmd.Run(); err != nil {
   116  		return fmt.Errorf("Error building devcam: %v", err)
   117  	}
   118  	return nil
   119  }
   120  
   121  func (c *testCmd) runTests() error {
   122  	args := []string{"test"}
   123  	if !strings.HasSuffix(c.buildGoPath, "-nosqlite") {
   124  		args = append(args, "--tags=with_sqlite")
   125  	}
   126  	if c.short {
   127  		args = append(args, "-short")
   128  	}
   129  	args = append(args, []string{
   130  		"./pkg/...",
   131  		"./server/camlistored",
   132  		"./server/appengine",
   133  		"./cmd/...",
   134  	}...)
   135  	env := c.env()
   136  	env.Set("SKIP_DEP_TESTS", "1")
   137  	return runExec("go", args, env)
   138  }