1
0
Fork 0
mirror of https://github.com/on4kjm/FLEcli.git synced 2025-02-18 12:51:02 +01:00
FLEcli/fleprocess/output_filename_test.go

126 lines
3.6 KiB
Go
Raw Normal View History

2020-07-29 08:22:13 +02:00
package fleprocess
2020-06-27 22:53:23 +02:00
2020-08-09 16:15:40 +02:00
/*
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-06-27 22:53:23 +02:00
import (
2020-08-09 18:01:38 +02:00
"errors"
2020-06-27 22:53:23 +02:00
"os"
"testing"
)
const testDir string = "test_dir"
const testFile string = "test.adi"
func setupTestCase(t *testing.T) func(t *testing.T) {
t.Log("setup test case")
//create test directory
if os.Mkdir(testDir, os.FileMode(0522)) != nil {
t.Error("Unexpected error creating testDir")
}
2020-06-27 22:53:23 +02:00
//create test file
f, _ := os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666)
defer f.Close()
return func(t *testing.T) {
t.Log("teardown test case")
//delete test directory
os.Remove(testDir)
//delete test file
2020-06-27 22:53:23 +02:00
os.Remove(testFile)
}
}
func Test_buildOutputFilename(t *testing.T) {
type args struct {
output string
input string
overwrite bool
2020-07-13 21:25:06 +02:00
extension string
2020-06-27 22:53:23 +02:00
}
tests := []struct {
name string
args args
wantOutputFilename string
2020-08-09 18:01:38 +02:00
wantError error
2020-06-27 22:53:23 +02:00
}{
{
"input file not provided",
2020-07-13 21:25:06 +02:00
args{input: "", output: "xxx", overwrite: false, extension: ".adi"},
"", errors.New("unexpected error: no input file provided"),
2020-06-27 22:53:23 +02:00
},
{
"Output file does not exist",
2020-07-13 21:25:06 +02:00
args{input: "a file", output: "output.adi", overwrite: false, extension: ".adi"},
2020-08-09 18:01:38 +02:00
"output.adi", nil,
2020-06-27 22:53:23 +02:00
},
{
"Output name is a directory",
2020-07-13 21:25:06 +02:00
args{input: "a file", output: testDir, overwrite: false, extension: ".adi"},
"", errors.New("error: specified output exists and is a directory"),
2020-06-27 22:53:23 +02:00
},
{
"Output exist but no overwrite",
2020-07-13 21:25:06 +02:00
args{input: "a file", output: testFile, overwrite: false, extension: ".adi"},
"", errors.New("file already exists. Use --overwrite flag if necessary"),
2020-08-09 18:01:38 +02:00
},
{
"Output exist but user wants to overwrite",
args{input: "a file", output: testFile, overwrite: true, extension: ".adi"},
"test.adi", nil,
2020-06-27 22:53:23 +02:00
},
{
"no output, input provided with extension",
2020-07-13 21:25:06 +02:00
args{input: "/test/data/file.txt", output: "", overwrite: false, extension: ".adi"},
2020-08-09 18:01:38 +02:00
"/test/data/file.adi", nil,
},
{
"no output, input provided without extension",
2020-07-13 21:25:06 +02:00
args{input: "/test/data/file", output: "", overwrite: false, extension: ".adi"},
2020-08-09 18:01:38 +02:00
"/test/data/file.adi", nil,
},
{
"no output, input provided, ending with a point",
2020-07-13 21:25:06 +02:00
args{input: "/test/data/file.", output: "", overwrite: false, extension: ".adi"},
2020-08-09 18:01:38 +02:00
"/test/data/file.adi", nil,
},
2020-06-27 22:53:23 +02:00
}
teardownTestCase := setupTestCase(t)
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-08-09 18:01:38 +02:00
gotOutputFilename, gotErr := buildOutputFilename(tt.args.output, tt.args.input, tt.args.overwrite, tt.args.extension)
2020-06-27 22:53:23 +02:00
if gotOutputFilename != tt.wantOutputFilename {
t.Errorf("buildOutputFilename() gotOutputFilename = %v, want %v", gotOutputFilename, tt.wantOutputFilename)
}
//test the error message, if any
2020-08-09 18:01:38 +02:00
if gotErr != nil && tt.wantError != nil {
if gotErr.Error() != tt.wantError.Error() {
t.Errorf("buildOutputFilename() error = %v, want %v", gotErr, tt.wantError)
}
} else {
if !(gotErr == nil && tt.wantError == nil) {
2020-08-09 18:01:38 +02:00
t.Errorf("buildOutputFilename() error = %v, want %v", gotErr, tt.wantError)
}
2020-06-27 22:53:23 +02:00
}
})
}
}