github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/extension/framework/framework.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, 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
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package framework
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"regexp"
    23  
    24  	"github.com/cloudwan/gohan/extension/framework/runner"
    25  	"github.com/codegangsta/cli"
    26  	logging "github.com/op/go-logging"
    27  )
    28  
    29  var log = logging.MustGetLogger("extest")
    30  
    31  func setUpLogging() {
    32  	backend := logging.NewLogBackend(os.Stderr, "", 0)
    33  	format := logging.MustStringFormatter(
    34  		"%{color}%{time:15:04:05.000}: %{module} %{level} %{color:reset} %{message}")
    35  	backendFormatter := logging.NewBackendFormatter(backend, format)
    36  	leveledBackendFormatter := logging.AddModuleLevel(backendFormatter)
    37  	leveledBackendFormatter.SetLevel(logging.CRITICAL, "")
    38  	leveledBackendFormatter.SetLevel(logging.DEBUG, "extest")
    39  	logging.SetBackend(leveledBackendFormatter)
    40  }
    41  
    42  // RunTests runs extension tests when invoked from Gohan CLI
    43  func RunTests(c *cli.Context) {
    44  	setUpLogging()
    45  
    46  	testFiles := getTestFiles(c.Args())
    47  	summary := map[string]error{}
    48  	for _, testFile := range testFiles {
    49  		log.Info("Running tests from '%s':", testFile)
    50  		testRunner := runner.NewTestRunner(testFile)
    51  		errors := testRunner.Run()
    52  		if err, ok := errors[runner.GeneralError]; ok {
    53  			summary[testFile] = fmt.Errorf("%s", err.Error())
    54  			log.Error(fmt.Sprintf("Error: %s", err.Error()))
    55  			continue
    56  		}
    57  
    58  		failed := 0
    59  		for test, err := range errors {
    60  			if err != nil {
    61  				failed = failed + 1
    62  				log.Error(fmt.Sprintf("\t FAIL (%s): %s", test, err.Error()))
    63  			} else if c.Bool("verbose") {
    64  				log.Notice("\t PASS (%s)", test)
    65  			}
    66  		}
    67  		summary[testFile] = nil
    68  		if failed > 0 {
    69  			summary[testFile] = fmt.Errorf("%d/%d tests failed", failed, len(errors))
    70  		}
    71  	}
    72  
    73  	returnCode := 0
    74  	log.Info("Run %d test files:", len(summary))
    75  	for testFile, err := range summary {
    76  		if err != nil {
    77  			returnCode = 1
    78  			log.Error(fmt.Sprintf("Failure in %s: %s", testFile, err.Error()))
    79  		} else {
    80  			log.Notice("OK %s ", testFile)
    81  		}
    82  	}
    83  	os.Exit(returnCode)
    84  }
    85  
    86  func getTestFiles(args cli.Args) []string {
    87  	paths := args
    88  	if len(paths) == 0 {
    89  		paths = append(paths, ".")
    90  	}
    91  
    92  	pattern := regexp.MustCompile(`^test_.*\.js$`)
    93  	seen := map[string]bool{}
    94  	testFiles := []string{}
    95  	for _, path := range paths {
    96  		filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
    97  			if err != nil {
    98  				log.Error(fmt.Sprintf("Failed to process '%s': %s", filePath, err.Error()))
    99  				return nil
   100  			}
   101  
   102  			if info.IsDir() {
   103  				return nil
   104  			}
   105  
   106  			if !pattern.MatchString(info.Name()) {
   107  				return nil
   108  			}
   109  
   110  			fullPath, err := filepath.Abs(filePath)
   111  			if err != nil {
   112  				log.Error(fmt.Sprintf("Failed to traverse file '%s': %s", fullPath, err.Error()))
   113  				return nil
   114  			}
   115  			fullPath = filepath.Clean(fullPath)
   116  
   117  			if !seen[fullPath] {
   118  				testFiles = append(testFiles, fullPath)
   119  				seen[fullPath] = true
   120  			}
   121  
   122  			return nil
   123  		})
   124  	}
   125  
   126  	return testFiles
   127  }