feat(actions): add more macro variables (#157)

* feat(actions): add more macro variables

* feat: add more macros

* feat: add more tests
This commit is contained in:
Ludvig Lundgren 2022-03-04 20:29:53 +01:00 committed by GitHub
parent e0e4bf6202
commit 5a45851677
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 252 additions and 68 deletions

View file

@ -1,14 +1,21 @@
package action
import (
"fmt"
"github.com/autobrr/autobrr/internal/domain"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestMacros_Parse(t *testing.T) {
currentTime := time.Now()
type fields struct {
TorrentName string
TorrentPathName string
TorrentUrl string
Indexer string
}
type args struct {
text string
@ -16,43 +23,60 @@ func TestMacros_Parse(t *testing.T) {
tests := []struct {
name string
fields fields
release domain.Release
args args
want string
wantErr bool
}{
{
name: "test_ok",
fields: fields{TorrentPathName: "/tmp/a-temporary-file.torrent"},
name: "test_ok",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "Print mee {{.TorrentPathName}}"},
want: "Print mee /tmp/a-temporary-file.torrent",
wantErr: false,
},
{
name: "test_bad",
fields: fields{TorrentPathName: "/tmp/a-temporary-file.torrent"},
name: "test_bad",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "Print mee {{TorrentPathName}}"},
want: "",
wantErr: true,
},
{
name: "test_program_arg",
fields: fields{TorrentPathName: "/tmp/a-temporary-file.torrent"},
name: "test_program_arg",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrentPathName}} --category test"},
want: "add /tmp/a-temporary-file.torrent --category test",
wantErr: false,
},
{
name: "test_program_arg_bad",
fields: fields{TorrentPathName: "/tmp/a-temporary-file.torrent"},
name: "test_program_arg_bad",
release: domain.Release{
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrenttPathName}} --category test"},
want: "",
wantErr: true,
},
{
name: "test_program_arg",
fields: fields{
TorrentName: "This movie 2021",
TorrentPathName: "/tmp/a-temporary-file.torrent",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrentPathName}} --category test --other {{.TorrentName}}"},
want: "add /tmp/a-temporary-file.torrent --category test --other This movie 2021",
@ -60,31 +84,87 @@ func TestMacros_Parse(t *testing.T) {
},
{
name: "test_args_long",
fields: fields{
release: domain.Release{
TorrentName: "This movie 2021",
TorrentUrl: "https://some.site/download/fakeid",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.TorrentName}} {{.TorrentUrl}} SOME_LONG_TOKEN"},
want: "This movie 2021 https://some.site/download/fakeid SOME_LONG_TOKEN",
wantErr: false,
},
{
name: "test_args_long_1",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}} {{.TorrentName}} {{.TorrentUrl}} SOME_LONG_TOKEN"},
want: "mock1 This movie 2021 https://some.site/download/fakeid SOME_LONG_TOKEN",
wantErr: false,
},
{
name: "test_args_category",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}}-race"},
want: "mock1-race",
wantErr: false,
},
{
name: "test_args_category_year",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}}-{{.Year}}-race"},
want: fmt.Sprintf("mock1-%v-race", currentTime.Year()),
wantErr: false,
},
{
name: "test_args_category_year",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: "DV",
},
args: args{text: "movies-{{.Resolution}}{{ if .HDR }}-{{.HDR}}{{ end }}"},
want: "movies-2160p-DV",
wantErr: false,
},
{
name: "test_args_category_and_if",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: "HDR",
},
args: args{text: "movies-{{.Resolution}}{{ if .HDR }}-{{.HDR}}{{ end }}"},
want: "movies-2160p-HDR",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := Macro{
TorrentPathName: tt.fields.TorrentPathName,
TorrentUrl: tt.fields.TorrentUrl,
TorrentName: tt.fields.TorrentName,
}
m := NewMacro(tt.release)
got, err := m.Parse(tt.args.text)
assert.Equal(t, currentTime.Year(), m.Year)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Parse() got = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got)
})
}
}