forked from lrstanley/go-ytdlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
183 lines (139 loc) · 4.4 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
//nolint:forbidigo
package ytdlp
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"testing"
"time"
)
type testSampleFile struct {
url string
name string
ext string
extractor string
}
var sampleFiles = []testSampleFile{
{url: "https://cdn.liam.sh/github/go-ytdlp/sample-1.mp4", name: "sample-1", ext: "mp4", extractor: "generic"},
{url: "https://cdn.liam.sh/github/go-ytdlp/sample-2.mp4", name: "sample-2", ext: "mp4", extractor: "generic"},
{url: "https://cdn.liam.sh/github/go-ytdlp/sample-3.mp4", name: "sample-3", ext: "mp4", extractor: "generic"},
{url: "https://cdn.liam.sh/github/go-ytdlp/sample-4.mpg", name: "sample-4", ext: "mpg", extractor: "generic"},
}
func TestCommand_Simple(t *testing.T) {
MustInstall(context.Background(), nil)
dir := t.TempDir()
var urls []string
for _, f := range sampleFiles {
urls = append(urls, f.url)
}
res, err := New().
Verbose().PrintJSON().NoProgress().NoOverwrites().Output(filepath.Join(dir, "%(extractor)s - %(title)s.%(ext)s")).
Run(context.Background(), urls...)
if err != nil {
t.Fatal(err)
}
if res == nil {
t.Fatal("res is nil")
}
if res.ExitCode != 0 {
t.Fatalf("expected exit code 0, got %d", res.ExitCode)
}
if !slices.Contains(res.Args, "--verbose") {
t.Fatal("expected --verbose flag to be set")
}
var hasJSON bool
for _, l := range res.OutputLogs {
if l.JSON != nil {
hasJSON = true
break
}
}
if !hasJSON {
t.Fatal("expected at least one log line to be valid JSON due to one of --print-json/--dump-json/--print '%()j'")
}
for _, f := range sampleFiles {
t.Run(f.name, func(t *testing.T) {
var stat fs.FileInfo
stat, err = os.Stat(filepath.Join(dir, fmt.Sprintf("%s - %s.%s", f.extractor, f.name, f.ext)))
if err != nil {
t.Fatal(err)
}
if stat.Size() == 0 {
t.Fatal("file is empty")
}
})
}
}
func TestCommand_Version(t *testing.T) {
MustInstall(context.Background(), nil)
res, err := New().Version(context.Background())
if err != nil {
t.Fatal(err)
}
if res == nil {
t.Fatal("res is nil")
}
if res.ExitCode != 0 {
t.Fatalf("expected exit code 0, got %d", res.ExitCode)
}
_, err = time.Parse("2006.01.02", res.Stdout)
if err != nil {
t.Fatalf("failed to parse version: %v", err)
}
}
func TestCommand_Unset(t *testing.T) {
MustInstall(context.Background(), nil)
builder := New().Progress().NoProgress().Output("test.mp4")
cmd := builder.buildCommand(context.TODO(), sampleFiles[0].url)
// Make sure --no-progress is set.
if !slices.Contains(cmd.Args, "--no-progress") {
t.Fatal("expected --no-progress flag to be set")
}
_ = builder.UnsetProgress()
cmd = builder.buildCommand(context.TODO(), sampleFiles[0].url)
// Make sure --no-progress is not set.
if slices.Contains(cmd.Args, "--no-progress") {
t.Fatal("expected --no-progress flag to not be set")
}
// Progress and NoProgress should conflict, so arg length should be 4 (executable, output, output value, and url).
if len(cmd.Args) != 4 {
t.Fatalf("expected arg length to be 4, got %d: %#v", len(cmd.Args), cmd.Args)
}
}
func TestCommand_Clone(t *testing.T) {
MustInstall(context.Background(), nil)
builder1 := New().NoProgress().Output("test.mp4")
builder2 := builder1.Clone()
cmd := builder2.buildCommand(context.TODO(), sampleFiles[0].url)
// Make sure --no-progress is set.
if !slices.Contains(cmd.Args, "--no-progress") {
t.Fatal("expected --no-progress flag to be set")
}
}
func TestCommand_SetExecutable(t *testing.T) {
MustInstall(context.Background(), nil)
cmd := New().SetExecutable("/usr/bin/test").buildCommand(context.Background(), sampleFiles[0].url)
if cmd.Path != "/usr/bin/test" {
t.Fatalf("expected executable to be /usr/bin/test, got %s", cmd.Path)
}
}
func TestCommand_SetWorkDir(t *testing.T) {
MustInstall(context.Background(), nil)
cmd := New().SetWorkDir("/tmp").buildCommand(context.Background(), sampleFiles[0].url)
if cmd.Dir != "/tmp" {
t.Fatalf("expected workdir to be /tmp, got %s", cmd.Dir)
}
}
func TestCommand_SetEnvVar(t *testing.T) {
MustInstall(context.Background(), nil)
cmd := New().SetEnvVar("TEST", "1").buildCommand(context.Background(), sampleFiles[0].url)
if cmd.Env[0] != "TEST=1" {
t.Fatalf("expected env var to be TEST=1, got %s", cmd.Env[0])
}
}