Skip to content

Commit

Permalink
add args test to datasetIngestor
Browse files Browse the repository at this point in the history
  • Loading branch information
consolethinks committed Jul 12, 2024
1 parent 513f3a7 commit d5494a7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
67 changes: 67 additions & 0 deletions cmd/commands/args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/paulscherrerinstitute/scicat/datasetUtils"
)

func TestArgs(t *testing.T) {
// test cases
tests := []struct {
name string
expectedArgs []interface{}
args []string
}{
// datasetIngestor
{
name: "datasetIngestor test with 1 param",
expectedArgs: []interface{}{
"some/place/metadata.json",
"",
"",
},
args: []string{"datasetIngestor", "some/place/metadata.json"},
},
{
name: "datasetIngestor test with 2 params - folderlisting.txt",
expectedArgs: []interface{}{
"some/place/metadata.json",
"",
"folderlisting.txt",
},
args: []string{"datasetIngestor", "some/place/metadata.json", "folderlisting.txt"},
},
{
name: "datasetIngestor test with 2 params - filelisting",
expectedArgs: []interface{}{
"some/place/metadata.json",
"/some/path/",
"",
},
args: []string{"datasetIngestor", "some/place/metadata.json", "/some/path/"},
},
}

// running test cases
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
//flag.CommandLine = flag.NewFlagSet(test.name, flag.ExitOnError)
datasetUtils.TestArgs = func(args []interface{}) {
passing := true
for i, _ := range test.expectedArgs {
if test.expectedArgs[i] != args[i] {
t.Logf("'%v' is not correct, expected: '%s'", args[i], test.expectedArgs[i])
passing = false
}
}
if !passing {
t.Fail()
}
}

rootCmd.SetArgs(test.args)
Execute()
})
}
}
43 changes: 23 additions & 20 deletions cmd/commands/datasetIngestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ For Windows you need instead to specify -user username:password on the command l
return
}

if len(args) <= 0 || len(args) >= 3 {
log.Fatal("invalid number of args")
}

metadatafile := args[0]
filelistingPath := ""
folderlistingPath := ""
absFileListing := ""
if len(args) == 2 {
if args[1] == "folderlisting.txt" {
folderlistingPath = args[1]
} else {
// NOTE filelistingPath is some kind of path to which the sourceFolder path should be relative
filelistingPath = args[1]
absFileListing, _ = filepath.Abs(filelistingPath)
}
}

if datasetUtils.TestArgs != nil {
datasetUtils.TestArgs([]interface{}{metadatafile, filelistingPath, folderlistingPath})
return
}

// functions use this flag in a way where "nil -> unset"
var allowExistingSourceFolderPtr *bool = &allowExistingSourceFolder
if !noninteractiveFlag && !cmd.Flags().Lookup("allowexistingsource").Changed {
Expand Down Expand Up @@ -141,26 +164,6 @@ For Windows you need instead to specify -user username:password on the command l
log.Printf("You are about to add a dataset to the === %s === data catalog environment...", env)
color.Unset()

metadatafile := ""
filelistingPath := ""
folderlistingPath := ""
absFileListing := ""

if len(args) <= 0 || len(args) >= 3 {
log.Fatal("invalid number of args")
}

metadatafile = args[0]
if len(args) == 2 {
if args[1] == "folderlisting.txt" {
folderlistingPath = args[1]
} else {
// NOTE filelistingPath is some kind of path to which the sourceFolder path should be relative
filelistingPath = args[1]
absFileListing, _ = filepath.Abs(filelistingPath)
}
}

// TODO: change pointer parameter types to values as they shouldn't be modified by the function
auth := &datasetUtils.RealAuthenticator{}
user, accessGroups := datasetUtils.Authenticate(auth, client, APIServer, &token, &userpass)
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions datasetUtils/testfunc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package datasetUtils

var TestFlags func(flags map[string]interface{}) = nil
var TestArgs func(args []interface{}) = nil

0 comments on commit d5494a7

Please sign in to comment.