github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camput/camput_test.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"runtime"
    23  	"testing"
    24  	"time"
    25  
    26  	"camlistore.org/pkg/cmdmain"
    27  )
    28  
    29  // env is the environment that a camput test runs within.
    30  type env struct {
    31  	// stdin is the standard input, or /dev/null if nil
    32  	stdin io.Reader
    33  
    34  	// TODO(bradfitz): vfs files.
    35  }
    36  
    37  func (e *env) Run(args ...string) (out, err []byte, exitCode int) {
    38  	outbuf := new(bytes.Buffer)
    39  	errbuf := new(bytes.Buffer)
    40  	cmdmain.Stdout, cmdmain.Stderr = outbuf, errbuf
    41  	exitc := make(chan int, 1)
    42  	cmdmain.Exit = func(code int) {
    43  		exitc <- code
    44  		runtime.Goexit()
    45  	}
    46  	go func() {
    47  		cmdmain.Main()
    48  		cmdmain.Exit(0)
    49  	}()
    50  	select {
    51  	case exitCode = <-exitc:
    52  	case <-time.After(15 * time.Second):
    53  		panic("timeout running command")
    54  	}
    55  	out = outbuf.Bytes()
    56  	err = errbuf.Bytes()
    57  	return
    58  }
    59  
    60  // TestUsageOnNoargs tests that we output a usage message when given no args, and return
    61  // with a non-zero exit status.
    62  func TestUsageOnNoargs(t *testing.T) {
    63  	var e env
    64  	out, err, code := e.Run()
    65  	if code != 1 {
    66  		t.Errorf("exit code = %d; want 1", code)
    67  	}
    68  	if len(out) != 0 {
    69  		t.Errorf("wanted nothing on stdout; got:\n%s", out)
    70  	}
    71  	if !bytes.Contains(err, []byte("Usage: camput")) {
    72  		t.Errorf("stderr doesn't contain usage. Got:\n%s", err)
    73  	}
    74  }
    75  
    76  func TestUploadingChangingDirectory(t *testing.T) {
    77  	// TODO(bradfitz):
    78  	//    $ mkdir /tmp/somedir
    79  	//    $ cp dev-camput /tmp/somedir
    80  	//    $ ./dev-camput  -file /tmp/somedir/ 2>&1 | tee /tmp/somedir/log
    81  	// ... verify it doesn't hang.
    82  	t.Logf("TODO")
    83  }