mirror of
https://github.com/zjdyzww/harness.git
synced 2026-09-28 14:24:00 +08:00
* 9d5d41 lint * b58a69 lint * 47a661 lint * 5ebf79 lint * 4ce1be Merge remote-tracking branch 'origin' into main-acm-cmxt * 5db8d4 messagE * fc54a0 coverage * 5e6026 fix: [CODE-4585]: fix default reviewers with codeowner file error * 10648b increase code coverage (#4483) (#4541) * f74698 increase code coverage (#4483) (#4498) * b1b395 increase code coverage (#4483) (#4487) * 951ba4 increase code coverage (#4483) * 8106d0 harness ACM created this fix
136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
// Copyright 2023 Harness, Inc.
|
|
//
|
|
// 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.
|
|
|
|
package cli
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetArguments(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
osArgs []string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "regular command with args",
|
|
osArgs: []string{"/path/to/gitness", "server", "start"},
|
|
expected: []string{"server", "start"},
|
|
},
|
|
{
|
|
name: "command with no args",
|
|
osArgs: []string{"/path/to/gitness"},
|
|
expected: []string{},
|
|
},
|
|
{
|
|
name: "command with single arg",
|
|
osArgs: []string{"/path/to/gitness", "version"},
|
|
expected: []string{"version"},
|
|
},
|
|
{
|
|
name: "command with multiple args",
|
|
osArgs: []string{"/path/to/gitness", "repo", "create", "myrepo"},
|
|
expected: []string{"repo", "create", "myrepo"},
|
|
},
|
|
{
|
|
name: "command with flags",
|
|
osArgs: []string{"/path/to/gitness", "server", "--port", "8080"},
|
|
expected: []string{"server", "--port", "8080"},
|
|
},
|
|
{
|
|
name: "command with mixed args and flags",
|
|
osArgs: []string{"/path/to/gitness", "repo", "create", "--private", "myrepo"},
|
|
expected: []string{"repo", "create", "--private", "myrepo"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Save original os.Args
|
|
originalArgs := os.Args
|
|
defer func() {
|
|
os.Args = originalArgs
|
|
}()
|
|
|
|
// Set test args
|
|
os.Args = tt.osArgs
|
|
|
|
// Call GetArguments
|
|
result := GetArguments()
|
|
|
|
// Verify result
|
|
assert.Equal(t, tt.expected, result, "arguments should match")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetArguments_PreservesOrder(t *testing.T) {
|
|
// Save original os.Args
|
|
originalArgs := os.Args
|
|
defer func() {
|
|
os.Args = originalArgs
|
|
}()
|
|
|
|
// Set test args with specific order
|
|
os.Args = []string{"/path/to/gitness", "first", "second", "third"}
|
|
|
|
// Call GetArguments
|
|
result := GetArguments()
|
|
|
|
// Verify order is preserved
|
|
expected := []string{"first", "second", "third"}
|
|
assert.Equal(t, expected, result, "argument order should be preserved")
|
|
}
|
|
|
|
func TestGetArguments_ReturnsSlice(t *testing.T) {
|
|
// Save original os.Args
|
|
originalArgs := os.Args
|
|
defer func() {
|
|
os.Args = originalArgs
|
|
}()
|
|
|
|
// Set test args
|
|
os.Args = []string{"/path/to/gitness", "arg1", "arg2"}
|
|
|
|
// Call GetArguments
|
|
result := GetArguments()
|
|
|
|
// Verify result is a slice
|
|
assert.NotNil(t, result, "result should not be nil")
|
|
assert.IsType(t, []string{}, result, "result should be a string slice")
|
|
}
|
|
|
|
func TestGetArguments_EmptyArgs(t *testing.T) {
|
|
// Save original os.Args
|
|
originalArgs := os.Args
|
|
defer func() {
|
|
os.Args = originalArgs
|
|
}()
|
|
|
|
// Set test args with only command
|
|
os.Args = []string{"/path/to/gitness"}
|
|
|
|
// Call GetArguments
|
|
result := GetArguments()
|
|
|
|
// Verify result is empty slice
|
|
assert.NotNil(t, result, "result should not be nil")
|
|
assert.Empty(t, result, "result should be empty")
|
|
assert.Equal(t, 0, len(result), "result length should be 0")
|
|
}
|