Use _test package to force use of public API

This commit is contained in:
Mattias Wadman
2021-12-21 16:49:52 +01:00
parent 30a347dec9
commit bfcb4c9c54

View File

@ -1,4 +1,4 @@
package goutubedl package goutubedl_test
// TODO: currently the tests only run on linux as they use osleaktest which only // TODO: currently the tests only run on linux as they use osleaktest which only
// has linux support // has linux support
@ -14,9 +14,15 @@ import (
"testing" "testing"
"github.com/fortytw2/leaktest" "github.com/fortytw2/leaktest"
"github.com/wader/goutubedl"
"github.com/wader/osleaktest" "github.com/wader/osleaktest"
) )
func init() {
// we're using yt-dlp at the moment
goutubedl.Path = "yt-dlp"
}
const testVideoRawURL = "https://www.youtube.com/watch?v=C0DPdy98e4c" const testVideoRawURL = "https://www.youtube.com/watch?v=C0DPdy98e4c"
const playlistRawURL = "https://soundcloud.com/mattheis/sets/kindred-phenomena" const playlistRawURL = "https://soundcloud.com/mattheis/sets/kindred-phenomena"
const subtitlesTestVideoRawURL = "https://www.youtube.com/watch?v=QRS8MkLhQmM" const subtitlesTestVideoRawURL = "https://www.youtube.com/watch?v=QRS8MkLhQmM"
@ -33,10 +39,10 @@ func leakChecks(t *testing.T) func() {
func TestBinaryNotPath(t *testing.T) { func TestBinaryNotPath(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
defer func(orig string) { Path = orig }(Path) defer func(orig string) { goutubedl.Path = orig }(goutubedl.Path)
Path = "/non-existing" goutubedl.Path = "/non-existing"
_, versionErr := Version(context.Background()) _, versionErr := goutubedl.Version(context.Background())
if versionErr == nil || !strings.Contains(versionErr.Error(), "no such file or directory") { if versionErr == nil || !strings.Contains(versionErr.Error(), "no such file or directory") {
t.Fatalf("err should be nil 'no such file or directory': %v", versionErr) t.Fatalf("err should be nil 'no such file or directory': %v", versionErr)
} }
@ -46,7 +52,7 @@ func TestVersion(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
versionRe := regexp.MustCompile(`^\d{4}\.\d{2}.\d{2}.*$`) versionRe := regexp.MustCompile(`^\d{4}\.\d{2}.\d{2}.*$`)
version, versionErr := Version(context.Background()) version, versionErr := goutubedl.Version(context.Background())
if versionErr != nil { if versionErr != nil {
t.Fatalf("err: %s", versionErr) t.Fatalf("err: %s", versionErr)
@ -61,7 +67,7 @@ func TestDownload(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
stderrBuf := &bytes.Buffer{} stderrBuf := &bytes.Buffer{}
r, err := New(context.Background(), testVideoRawURL, Options{ r, err := goutubedl.New(context.Background(), testVideoRawURL, goutubedl.Options{
StderrFn: func(cmd *exec.Cmd) io.Writer { StderrFn: func(cmd *exec.Cmd) io.Writer {
return stderrBuf return stderrBuf
}, },
@ -84,8 +90,8 @@ func TestDownload(t *testing.T) {
t.Errorf("copy n not equal to download buffer: %d!=%d", n, downloadBuf.Len()) t.Errorf("copy n not equal to download buffer: %d!=%d", n, downloadBuf.Len())
} }
if n < 29000 { if n < 10000 {
t.Errorf("should have copied at least 29000 bytes: %d", n) t.Errorf("should have copied at least 10000 bytes: %d", n)
} }
if !strings.Contains(stderrBuf.String(), "Destination") { if !strings.Contains(stderrBuf.String(), "Destination") {
@ -106,7 +112,7 @@ func TestParseInfo(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
ctx, cancelFn := context.WithCancel(context.Background()) ctx, cancelFn := context.WithCancel(context.Background())
ydlResult, err := New(ctx, c.url, Options{ ydlResult, err := goutubedl.New(ctx, c.url, goutubedl.Options{
DownloadThumbnail: true, DownloadThumbnail: true,
}) })
if err != nil { if err != nil {
@ -156,8 +162,8 @@ func TestParseInfo(t *testing.T) {
func TestPlaylist(t *testing.T) { func TestPlaylist(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{ ydlResult, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
Type: TypePlaylist, Type: goutubedl.TypePlaylist,
DownloadThumbnail: false, DownloadThumbnail: false,
}) })
@ -184,7 +190,7 @@ func TestPlaylist(t *testing.T) {
func TestTestUnsupportedURL(t *testing.T) { func TestTestUnsupportedURL(t *testing.T) {
defer leaktest.Check(t)() defer leaktest.Check(t)()
_, ydlResultErr := New(context.Background(), "https://www.google.com", Options{}) _, ydlResultErr := goutubedl.New(context.Background(), "https://www.google.com", goutubedl.Options{})
if ydlResultErr == nil { if ydlResultErr == nil {
t.Errorf("expected unsupported url") t.Errorf("expected unsupported url")
} }
@ -199,8 +205,8 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
defer leaktest.Check(t)() defer leaktest.Check(t)()
playlistRawURL := "https://www.youtube.com/playlist?list=PLX0g748fkegS54oiDN4AXKl7BR7mLIydP" playlistRawURL := "https://www.youtube.com/playlist?list=PLX0g748fkegS54oiDN4AXKl7BR7mLIydP"
ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{ ydlResult, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
Type: TypePlaylist, Type: goutubedl.TypePlaylist,
DownloadThumbnail: false, DownloadThumbnail: false,
}) })
@ -218,10 +224,10 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
func TestSubtitles(t *testing.T) { func TestSubtitles(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
ydlResult, ydlResultErr := New( ydlResult, ydlResultErr := goutubedl.New(
context.Background(), context.Background(),
subtitlesTestVideoRawURL, subtitlesTestVideoRawURL,
Options{ goutubedl.Options{
DownloadSubtitles: true, DownloadSubtitles: true,
}) })
@ -250,11 +256,11 @@ func TestSubtitles(t *testing.T) {
func TestErrorNotAPlaylist(t *testing.T) { func TestErrorNotAPlaylist(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
_, ydlResultErr := New(context.Background(), testVideoRawURL, Options{ _, ydlResultErr := goutubedl.New(context.Background(), testVideoRawURL, goutubedl.Options{
Type: TypePlaylist, Type: goutubedl.TypePlaylist,
DownloadThumbnail: false, DownloadThumbnail: false,
}) })
if ydlResultErr != ErrNotAPlaylist { if ydlResultErr != goutubedl.ErrNotAPlaylist {
t.Errorf("expected is playlist error, got %s", ydlResultErr) t.Errorf("expected is playlist error, got %s", ydlResultErr)
} }
} }
@ -262,11 +268,11 @@ func TestErrorNotAPlaylist(t *testing.T) {
func TestErrorNotASingleEntry(t *testing.T) { func TestErrorNotASingleEntry(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
_, ydlResultErr := New(context.Background(), playlistRawURL, Options{ _, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
Type: TypeSingle, Type: goutubedl.TypeSingle,
DownloadThumbnail: false, DownloadThumbnail: false,
}) })
if ydlResultErr != ErrNotASingleEntry { if ydlResultErr != goutubedl.ErrNotASingleEntry {
t.Errorf("expected is single entry error, got %s", ydlResultErr) t.Errorf("expected is single entry error, got %s", ydlResultErr)
} }
} }