github.com/upyun/upx@v0.4.7-0.20240419023638-b184a7cb7c10/upx_test.go (about)

     1  package upx
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  var (
    16  	ROOT     = fmt.Sprintf("/upx-test/%s", time.Now())
    17  	BUCKET_1 = os.Getenv("UPYUN_BUCKET1")
    18  	BUCKET_2 = os.Getenv("UPYUN_BUCKET2")
    19  	USERNAME = os.Getenv("UPYUN_USERNAME")
    20  	PASSWORD = os.Getenv("UPYUN_PASSWORD")
    21  )
    22  
    23  func SetUp() {
    24  	Upx("login", BUCKET_1, USERNAME, PASSWORD)
    25  }
    26  
    27  func TearDown() {
    28  	for {
    29  		b, err := Upx("logout")
    30  		if err != nil || string(b) == "Nothing to do ~\n" {
    31  			break
    32  		}
    33  	}
    34  }
    35  
    36  func CreateFile(fpath string) {
    37  	os.MkdirAll(filepath.Dir(fpath), 0755)
    38  	fd, _ := os.Create(fpath)
    39  	fd.WriteString("UPX")
    40  	fd.Close()
    41  }
    42  
    43  func Upx(args ...string) ([]byte, error) {
    44  	cmd := exec.Command("upx", args...)
    45  	var obuf, ebuf bytes.Buffer
    46  	cmd.Stdout, cmd.Stderr = &obuf, &ebuf
    47  	if err := cmd.Start(); err != nil {
    48  		return nil, err
    49  	}
    50  	err := cmd.Wait()
    51  	ob, _ := ioutil.ReadAll(&obuf)
    52  	eb, _ := ioutil.ReadAll(&ebuf)
    53  	if err != nil {
    54  		return ob, fmt.Errorf("%s", string(eb))
    55  	}
    56  	return ob, nil
    57  }
    58  
    59  func TestMain(m *testing.M) {
    60  	pwd, _ := os.Getwd()
    61  	os.Setenv("PATH", pwd)
    62  	flag.Parse()
    63  	code := m.Run()
    64  	os.Exit(code)
    65  }