github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/wordsmith/utility/command_test.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under 
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>. 
     9  //
    10  // https://documize.com
    11  
    12  package utility
    13  
    14  import "testing"
    15  import "os/exec"
    16  import "time"
    17  
    18  func TestCmd(t *testing.T) {
    19  	cmd := exec.Command("echo", "test")
    20  	buf, err := CommandWithTimeout(cmd,time.Second)
    21  	if err != nil {
    22  		t.Error(err)
    23  		return
    24  	}
    25  	if string(buf) != "test\n" {
    26  		t.Error("command did not return `test` it returned:" + string(buf))
    27  	}
    28  	cmd2 := exec.Command("dingbat doodah")
    29  	_, err2 := CommandWithTimeout(cmd2,time.Second)
    30  	if err2 == nil {
    31  		t.Error("bad command did not return an error")
    32  	}
    33  	timeout := 5 * time.Second
    34  	cmd3 := exec.Command("sleep", "50")
    35  	_, err3 := CommandWithTimeout(cmd3,timeout)
    36  	if err3 != errTimeout {
    37  		t.Error("sleep command did not timeout:", err3)
    38  	}
    39  }