github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/testing/shell.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"strings"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  )
    12  
    13  // CheckWriteFileCommand verifies that the given shell command
    14  // correctly writes the expected content to the given filename. The
    15  // provided parse function decomposes file content into structured data
    16  // that may be correctly compared regardless of ordering within the
    17  // content. If parse is nil then the content lines are used un-parsed.
    18  func CheckWriteFileCommand(c *gc.C, cmd, filename, expected string, parse func(lines []string) interface{}) {
    19  	if parse == nil {
    20  		parse = func(lines []string) interface{} {
    21  			return lines
    22  		}
    23  	}
    24  
    25  	lines := strings.Split(strings.TrimSpace(cmd), "\n")
    26  	header := lines[0]
    27  	footer := lines[len(lines)-1]
    28  	parsed := parse(lines[1 : len(lines)-1])
    29  
    30  	// Check the cat portion.
    31  	c.Check(header, gc.Equals, "cat > "+filename+" << 'EOF'")
    32  	c.Check(footer, gc.Equals, "EOF")
    33  
    34  	// Check the conf portion.
    35  	expectedParsed := parse(strings.Split(expected, "\n"))
    36  	c.Check(parsed, jc.DeepEquals, expectedParsed)
    37  }