tdl/test/suite_test.go
Junyu Liu 5451bcb1ad
fix(test): Telegram test servers no longer provide test accounts (#753)
* runs on latest ubuntu

* runs on latest ubuntu

* cancel inf retries

* change to self-hosted account

* fix missing args

* fix opendb error

* inf retries

* inf retries
2024-10-17 17:05:57 +08:00

71 lines
1.2 KiB
Go

package test
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/fatih/color"
"github.com/spf13/cobra"
tcmd "github.com/iyear/tdl/cmd"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestCommand(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Test tdl")
}
var (
cmd *cobra.Command
args []string
output string
storage string
)
var _ = BeforeSuite(func() {
// used to avoid "open db: timeout" conflict
storage = fmt.Sprintf("type=file,path=%s",
filepath.Join(os.TempDir(), "tdl", strconv.FormatInt(time.Now().UnixNano(), 10)))
log.SetOutput(GinkgoWriter)
})
var _ = BeforeEach(func() {
cmd = tcmd.New()
// wait before each test to avoid rate limit
time.Sleep(10 * time.Second)
})
func exec(cmd *cobra.Command, args []string, success bool) {
r, w, err := os.Pipe()
Expect(err).To(Succeed())
os.Stdout = w
color.Output = w
log.Printf("args: %s\n", args)
cmd.SetArgs(append([]string{
"--storage", storage,
}, args...))
if err = cmd.Execute(); success {
Expect(err).To(Succeed())
} else {
Expect(err).ToNot(Succeed())
}
Expect(w.Close()).To(Succeed())
o, err := io.ReadAll(r)
Expect(err).To(Succeed())
output = string(o)
}