Home > front end >  How do i test a function which uses filepath.walk
How do i test a function which uses filepath.walk

Time:08-12

I have a function which gets all PDF files in a directory and returns the files if there are some.

func GetPdfFiles(path string) ([]string, error) {
var files []string
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
    if strings.HasSuffix(path, ".pdf") {
        files = append(files, path)
    }
    return nil
})
if err != nil {
    return nil, err
}
if files == nil {
    return nil, errors.New("No PdfFiles found.")
}
return files, nil
}

My Test function gets the error: nil, from the filepath.Walk function which requires a anonymous function that returns an error, but it should get the error from the if statements like in the case of the second testcase with no files it should return errors.New("No PdfFiles found."). How can i test it correctly.

func TestGetPdfFiles(t *testing.T) {
type args struct {
    path string
}

cwd, err := os.Getwd()
if err != nil {
    log.Fatal(err)
}

tests := []struct {
    name    string
    args    args
    want    []string
    wantErr bool
}{
    {name: "2PdfFiles", args: args{path: fmt.Sprintf("%s/testData/", cwd)}, want: []string{fmt.Sprintf("%s/testData/test-1.pdf", cwd), fmt.Sprintf("%s/testData/test-2.pdf", cwd)}, wantErr: false},
    {name: "noPdfFiles", args: args{path: fmt.Sprintf("%s", cwd)}, want: nil, wantErr: true},
}
for _, tt := range tests {
    t.Run(tt.name, func(t *testing.T) {
        got, err := GetPdfFiles(tt.args.path)
        if (err != nil) != tt.wantErr {
            t.Errorf("GetPdfFiles() error = %v, wantErr %v", err, tt.wantErr)
            return
        }
        if !reflect.DeepEqual(got, tt.want) {
            t.Errorf("GetPdfFiles() got = %v, want %v", got, tt.want)
        }
    })
}
}

CodePudding user response:

How about using T.TempDir() for a unique empty directory every time. This will guarantee repeatable results. The testing package will also take care of cleaning that:

func TestGetPdfFiles(t *testing.T) {
    type args struct {
        path string
    }

    tests := []struct {
        name    string
        args    args
        want    []string
        wantErr bool
    }{
        {name: "noPdfFiles", args: args{path: t.TempDir()}, want: nil, wantErr: true},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := GetPdfFiles(tt.args.path)
            if (err != nil) != tt.wantErr {
                t.Errorf("GetPdfFiles() error = %v, wantErr %v", err, tt.wantErr)
                return
            }
            if !reflect.DeepEqual(got, tt.want) {
                t.Errorf("GetPdfFiles() got = %v, want %v", got, tt.want)
            }
        })
    }
}

CodePudding user response:

I guess, there isn't really a good way to do it. so i removed the error checks and just return an empty array, it would've been too messy. Also thanks to @Qasim Sarfraz for the T.TempDir idea.

func GetPdfFiles(path string) []string {
    var files []string
    filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
        if strings.HasSuffix(path, ".pdf") {
            files = append(files, path)
        }
        return nil
    })
    return files
}

func TestGetPdfFiles(t *testing.T) {
    type args struct {
        path string
    }

    cwd, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }

    tests := []struct {
        name    string
        args    args
        want    []string
        wantErr bool
    }{
        {name: "2PdfFiles", args: args{path: fmt.Sprintf("%s/testData/", cwd)}, want: []string{fmt.Sprintf("%s/testData/test-1.pdf", cwd), fmt.Sprintf("%s/testData/test-2.pdf", cwd)}},
        {name: "noPdfFiles", args: args{path: t.TempDir()}, want: nil},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := GetPdfFiles(tt.args.path)
            if !reflect.DeepEqual(got, tt.want) {
                t.Errorf("GetPdfFiles() got = %v, want %v", got, tt.want)
            }
        })
    }
}
  • Related