github.com/osievert/jfrog-cli-core@v1.2.7/artifactory/commands/dotnet/dotnetcommand_test.go (about)

     1  package dotnet
     2  
     3  import (
     4  	"github.com/jfrog/gofrog/io"
     5  	"github.com/jfrog/jfrog-cli-core/artifactory/utils/dotnet"
     6  	"github.com/stretchr/testify/assert"
     7  	"os"
     8  	"path/filepath"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestGetFlagValueExists(t *testing.T) {
    14  	tests := []struct {
    15  		name              string
    16  		currentConfigPath string
    17  		createConfig      bool
    18  		expectErr         bool
    19  		cmdFlags          []string
    20  		expectedCmdFlags  []string
    21  	}{
    22  		{"simple", "file.config", true, false,
    23  			[]string{"-configFile", "file.config"}, []string{"-configFile", "file.config"}},
    24  
    25  		{"simple2", "file.config", true, false,
    26  			[]string{"-before", "-configFile", "file.config", "after"}, []string{"-before", "-configFile", "file.config", "after"}},
    27  
    28  		{"err", "file.config", false, true,
    29  			[]string{"-before", "-configFile"}, []string{"-before", "-configFile"}},
    30  
    31  		{"err2", "file.config", false, true,
    32  			[]string{"-configFile"}, []string{"-configFile"}},
    33  	}
    34  	for _, test := range tests {
    35  		t.Run(test.name, func(t *testing.T) {
    36  			if test.createConfig {
    37  				_, err := io.CreateRandFile(test.currentConfigPath, 0)
    38  				if err != nil {
    39  					t.Error(err)
    40  				}
    41  				defer os.Remove(test.currentConfigPath)
    42  			}
    43  			c := &dotnet.Cmd{CommandFlags: test.cmdFlags}
    44  			_, err := getFlagValueIfExists("-configfile", c)
    45  			if err != nil && !test.expectErr {
    46  				t.Error(err)
    47  			}
    48  			if err == nil && test.expectErr {
    49  				t.Errorf("Expecting: error, Got: nil")
    50  			}
    51  			if !reflect.DeepEqual(c.CommandFlags, test.expectedCmdFlags) {
    52  				t.Errorf("Expecting: %s, Got: %s", test.expectedCmdFlags, c.CommandFlags)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestUpdateSolutionPathAndGetFileName(t *testing.T) {
    59  	workingDir, err := os.Getwd()
    60  	assert.NoError(t, err)
    61  	tests := []struct {
    62  		name                 string
    63  		flags                []string
    64  		solutionPath         string
    65  		expectedSlnFile      string
    66  		expectedSolutionPath string
    67  	}{
    68  		{"emptyFlags", []string{}, workingDir, "", workingDir},
    69  		{"justFlags", []string{"-flag1", "value1", "-flag2", "value2"}, workingDir, "", workingDir},
    70  		{"relFileArgRelPath1", []string{filepath.Join("testdata", "slnDir", "sol.sln")}, filepath.Join("rel", "path"), "sol.sln", filepath.Join("rel", "path", "testdata", "slnDir")},
    71  		{"relDirArgRelPath2", []string{filepath.Join("testdata", "slnDir")}, filepath.Join("rel", "path"), "", filepath.Join("rel", "path", "testdata", "slnDir")},
    72  		{"absFileArgRelPath1", []string{filepath.Join(workingDir, "testdata", "slnDir", "sol.sln")}, filepath.Join(".", "rel", "path"), "sol.sln", filepath.Join(workingDir, "testdata", "slnDir")},
    73  		{"absDirArgRelPath2", []string{filepath.Join(workingDir, "testdata", "slnDir"), "-flag", "value"}, filepath.Join(".", "rel", "path"), "", filepath.Join(workingDir, "testdata", "slnDir")},
    74  		{"nonExistingFile", []string{filepath.Join(".", "dir1", "sol.sln")}, workingDir, "", workingDir},
    75  		{"nonExistingPath", []string{filepath.Join(workingDir, "non", "existing", "path")}, workingDir, "", workingDir},
    76  		{"relCsprojFile", []string{filepath.Join("testdata", "slnDir", "proj.csproj")}, filepath.Join("rel", "path"), "", filepath.Join("rel", "path", "testdata", "slnDir")},
    77  		{"relVbprojFile", []string{filepath.Join("testdata", "slnDir", "projTwo.vbproj")}, filepath.Join("rel", "path"), "", filepath.Join("rel", "path", "testdata", "slnDir")},
    78  		{"absCsprojFile", []string{filepath.Join(workingDir, "testdata", "slnDir", "proj.csproj")}, filepath.Join("rel", "path"), "", filepath.Join(workingDir, "testdata", "slnDir")},
    79  		{"absVbprojFile", []string{filepath.Join(workingDir, "testdata", "slnDir", "projTwo.vbproj")}, filepath.Join("rel", "path"), "", filepath.Join(workingDir, "testdata", "slnDir")},
    80  		{"relPackagesConfigFile", []string{filepath.Join("testdata", "slnDir", "packages.config")}, filepath.Join("rel", "path"), "", filepath.Join("rel", "path", "testdata", "slnDir")},
    81  		{"absPackagesConfigFile", []string{filepath.Join(workingDir, "testdata", "slnDir", "packages.config")}, filepath.Join("rel", "path"), "", filepath.Join(workingDir, "testdata", "slnDir")},
    82  	}
    83  	for _, test := range tests {
    84  		t.Run(test.name, func(t *testing.T) {
    85  			dc := DotnetCommand{solutionPath: test.solutionPath, argAndFlags: test.flags}
    86  			slnFile, err := dc.updateSolutionPathAndGetFileName()
    87  			assert.NoError(t, err)
    88  			assert.Equal(t, test.expectedSlnFile, slnFile)
    89  			assert.Equal(t, test.expectedSolutionPath, dc.solutionPath)
    90  		})
    91  	}
    92  }