github.com/coreos/mantle@v0.13.0/harness/_example/main.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/coreos/mantle/harness"
    23  )
    24  
    25  type X struct {
    26  	*harness.H
    27  	defaults map[string]string
    28  }
    29  
    30  func (x *X) Option(key string) string {
    31  	env := "TEST_DATA_" + key
    32  	if value := os.Getenv(env); value != "" {
    33  		return value
    34  	}
    35  
    36  	if value, ok := x.defaults[key]; ok {
    37  		return value
    38  	}
    39  
    40  	x.Skipf("Missing %q in environment.", env)
    41  	return ""
    42  }
    43  
    44  type Test struct {
    45  	Name     string
    46  	Run      func(x *X)
    47  	Defaults map[string]string
    48  }
    49  
    50  var tests harness.Tests
    51  
    52  func Register(test Test) {
    53  	// copy map to prevent surprises
    54  	defaults := make(map[string]string)
    55  	for k, v := range test.Defaults {
    56  		defaults[k] = v
    57  	}
    58  
    59  	tests.Add(test.Name, func(h *harness.H) {
    60  		test.Run(&X{H: h, defaults: defaults})
    61  	})
    62  }
    63  
    64  func main() {
    65  	opts := harness.Options{OutputDir: "_x_temp"}
    66  	opts.FlagSet("", flag.ExitOnError).Parse(os.Args[1:])
    67  
    68  	suite := harness.NewSuite(opts, tests)
    69  	if err := suite.Run(); err != nil {
    70  		fmt.Fprintln(os.Stderr, err)
    71  		fmt.Println("FAIL")
    72  		os.Exit(1)
    73  	}
    74  	fmt.Println("PASS")
    75  	os.Exit(0)
    76  }