You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.9 KiB

package main
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestIsInSpecialDir(t *testing.T) {
special := []string{
filepath.Join(absBasePath, "series", "media1", "file"),
filepath.Join(absBasePath, "film", "file"),
filepath.Join(absBasePath, "music", "album", "file"),
filepath.Join(absBasePath, "programs", "file"),
}
for _, s := range special {
if !isInSpecialDir(s) {
t.Errorf("Should be special: %s\n", s)
}
}
other := []string{
filepath.Join(absBasePath, "media1", "film", "file"),
filepath.Join(absBasePath, "aFilm", "film", "file"),
filepath.Join(absBasePath, "album", "music", "file"),
filepath.Join(absBasePath, "myProgram", "program", "theProgram.exe"),
}
for _, o := range other {
if isInSpecialDir(o) {
t.Errorf("Should be special: %s\n", o)
}
}
}
func TestBaseDirOrFile(t *testing.T) {
data := []struct {
name string
expected string
}{
{
name: filepath.Join(absBasePath, "file"),
expected: filepath.Join(absBasePath, "file"),
},
{
name: filepath.Join(absBasePath, "baseDir", "file"),
expected: filepath.Join(absBasePath, "baseDir"),
},
{
name: filepath.Join(absBasePath, "baseDir", "otherDir", "file"),
expected: filepath.Join(absBasePath, "baseDir"),
},
}
for _, datum := range data {
if res := baseDirOrFile(datum.name); res != datum.expected {
t.Errorf("%s != %s\n", res, datum.expected)
}
}
}
func TestLink(t *testing.T) {
dir, err := os.MkdirTemp("/tmp", "classifier-test-*")
if err != nil {
t.Fatal(err)
}
for _, d := range []string{"series", "film", "music", "programs"} {
if err := os.Mkdir(filepath.Join(dir, d), 0700); err != nil {
t.Fatal(err)
}
}
testDirs := []struct {
path string
kind kindOf
}{
{path: filepath.Join(dir, "testFilm1"), kind: film},
{path: filepath.Join(dir, "testSeries"), kind: series},
{path: filepath.Join(dir, "testAlbum"), kind: music},
{path: filepath.Join(dir, "testProgram1"), kind: program},
}
for _, d := range testDirs {
err := os.Mkdir(d.path, 0700)
if err != nil {
t.Fatal(err)
}
err = link(dir, d.path, d.kind)
if err != nil {
return
}
}
testFiles := []struct {
path string
kind kindOf
}{
{path: filepath.Join(dir, "testFilm2.avi"), kind: film},
{path: filepath.Join(dir, "testProgram2.exe"), kind: program},
}
for _, f := range testFiles {
fh, err := os.Create(f.path)
if err != nil {
t.Fatal(err)
}
fh.Close()
err = link(dir, f.path, f.kind)
if err != nil {
return
}
}
fFilm, err := ioutil.ReadDir(filepath.Join(dir, "film"))
if err != nil {
t.Fatal(err)
}
sFilm := []string{}
for _, f := range fFilm {
sFilm = append(sFilm, f.Name())
}
if reflect.DeepEqual(sFilm, []string{
filepath.Join(dir, "film", "testFilm1"),
filepath.Join(dir, "film", "testFilm2.avi"),
}) {
t.Errorf("Unexpected film links: %s\n", sFilm)
}
fSeries, err := ioutil.ReadDir(filepath.Join(dir, "series"))
if err != nil {
t.Fatal(err)
}
sSeries := []string{}
for _, f := range fSeries {
sSeries = append(sSeries, f.Name())
}
if reflect.DeepEqual(sFilm, []string{
filepath.Join(dir, "film", "testSeries"),
}) {
t.Errorf("Unexpected series links: %s\n", sFilm)
}
fMusic, err := ioutil.ReadDir(filepath.Join(dir, "music"))
if err != nil {
t.Fatal(err)
}
sMusic := []string{}
for _, f := range fMusic {
sMusic = append(sMusic, f.Name())
}
if reflect.DeepEqual(sMusic, []string{
filepath.Join(dir, "film", "testAlbum"),
}) {
t.Errorf("Unexpected music links: %s\n", sMusic)
}
fProg, err := ioutil.ReadDir(filepath.Join(dir, "film"))
if err != nil {
t.Fatal(err)
}
sProg := []string{}
for _, f := range fProg {
sProg = append(sProg, f.Name())
}
if reflect.DeepEqual(sProg, []string{
filepath.Join(dir, "film", "testProgram1"),
filepath.Join(dir, "film", "testProgram2.exe"),
}) {
t.Errorf("Unexpected programs links: %s\n", sProg)
}
os.RemoveAll(dir)
}