...

Source file src/github.com/joshdk/go-junit/ingesters.go

Documentation: github.com/joshdk/go-junit

     1  // Copyright Josh Komoroske. All rights reserved.
     2  // Use of this source code is governed by the MIT license,
     3  // a copy of which can be found in the LICENSE.txt file.
     4  
     5  package junit
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  )
    14  
    15  // IngestDir will search the given directory for XML files and return a slice
    16  // of all contained JUnit test suite definitions.
    17  func IngestDir(directory string) ([]Suite, error) {
    18  	var filenames []string
    19  
    20  	err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
    21  		if err != nil {
    22  			return err
    23  		}
    24  
    25  		// Add all regular files that end with ".xml"
    26  		if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".xml") {
    27  			filenames = append(filenames, path)
    28  		}
    29  
    30  		return nil
    31  	})
    32  
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	return IngestFiles(filenames)
    38  }
    39  
    40  // IngestFiles will parse the given XML files and return a slice of all
    41  // contained JUnit test suite definitions.
    42  func IngestFiles(filenames []string) ([]Suite, error) {
    43  	var all = make([]Suite, 0)
    44  
    45  	for _, filename := range filenames {
    46  		suites, err := IngestFile(filename)
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		all = append(all, suites...)
    51  	}
    52  
    53  	return all, nil
    54  }
    55  
    56  // IngestFile will parse the given XML file and return a slice of all contained
    57  // JUnit test suite definitions.
    58  func IngestFile(filename string) ([]Suite, error) {
    59  	file, err := os.Open(filename)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	defer file.Close()
    64  
    65  	return IngestReader(file)
    66  }
    67  
    68  // IngestReader will parse the given XML reader and return a slice of all
    69  // contained JUnit test suite definitions.
    70  func IngestReader(reader io.Reader) ([]Suite, error) {
    71  	var (
    72  		suiteChan = make(chan Suite)
    73  		suites    = make([]Suite, 0)
    74  	)
    75  
    76  	nodes, err := parse(reader)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	go func() {
    82  		findSuites(nodes, suiteChan)
    83  		close(suiteChan)
    84  	}()
    85  
    86  	for suite := range suiteChan {
    87  		suites = append(suites, suite)
    88  	}
    89  
    90  	return suites, nil
    91  }
    92  
    93  // Ingest will parse the given XML data and return a slice of all contained
    94  // JUnit test suite definitions.
    95  func Ingest(data []byte) ([]Suite, error) {
    96  	return IngestReader(bytes.NewReader(data))
    97  }
    98  

View as plain text