-
Notifications
You must be signed in to change notification settings - Fork 2.4k
drop shlex dependency #5943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 4 commits into
kubernetes-sigs:master
from
koba1t:chore/drop_shlex_dependency
Jul 20, 2025
Merged
drop shlex dependency #5943
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2019 The Kubernetes Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package execplugin | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "unicode" | ||
| ) | ||
|
|
||
| // ShlexSplit splits a string into a slice of strings using shell-style rules for quoting and commenting | ||
| // Similar to Python's shlex.split with comments enabled | ||
| func ShlexSplit(s string) ([]string, error) { | ||
| return shlexSplit(s) | ||
| } | ||
|
|
||
| func shlexSplit(s string) ([]string, error) { | ||
| result := []string{} | ||
|
|
||
| // noQuote is used to track if we are not in a quoted | ||
| const noQuote = 0 | ||
|
|
||
| var current strings.Builder | ||
| var quote rune = noQuote | ||
| var escaped bool | ||
|
|
||
| for _, r := range s { | ||
| switch { | ||
| case escaped: | ||
| current.WriteRune(r) | ||
| escaped = false | ||
| case r == '\\' && quote != '\'': | ||
| escaped = true | ||
| case (r == '\'' || r == '"') && quote == noQuote: | ||
| quote = r | ||
| case r == quote: | ||
| quote = noQuote | ||
| case r == '#' && quote == noQuote: | ||
| // Comment starts, ignore the rest of the line | ||
| if current.Len() > 0 { | ||
| result = append(result, current.String()) | ||
| } | ||
| return result, nil | ||
| case unicode.IsSpace(r) && quote == noQuote: | ||
| if current.Len() > 0 { | ||
| result = append(result, current.String()) | ||
| current.Reset() | ||
| } | ||
| default: | ||
| current.WriteRune(r) | ||
| } | ||
| } | ||
|
|
||
| if quote != noQuote { | ||
| return nil, fmt.Errorf("unclosed quote in string") | ||
| } | ||
| if current.Len() > 0 { | ||
| result = append(result, current.String()) | ||
| } | ||
| return result, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Copyright 2019 The Kubernetes Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package execplugin | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestShlexSplit(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| input string | ||
| expected []string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "basic space separation", | ||
| input: `hello world`, | ||
| expected: []string{"hello", "world"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "double quoted string", | ||
| input: `"hello world"`, | ||
| expected: []string{"hello world"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "single quoted string", | ||
| input: `'hello world'`, | ||
| expected: []string{"hello world"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "mixed quotes and words", | ||
| input: `hello "world test"`, | ||
| expected: []string{"hello", "world test"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "single quotes with spaces", | ||
| input: `hello 'world test'`, | ||
| expected: []string{"hello", "world test"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "nested quotes - single in double", | ||
| input: `"hello 'nested' world"`, | ||
| expected: []string{"hello 'nested' world"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "nested quotes - double in single", | ||
| input: `'hello "nested" world'`, | ||
| expected: []string{"hello \"nested\" world"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "escaped space", | ||
| input: `hello\ world`, | ||
| expected: []string{"hello world"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "escaped quotes in double quotes", | ||
| input: `"hello \"world\""`, | ||
| expected: []string{"hello \"world\""}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "single quote in single quotes", | ||
| input: `'can'\''t'`, | ||
| expected: []string{"can't"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "complex argument list", | ||
| input: `arg1 "arg 2" 'arg 3' arg4`, | ||
| expected: []string{"arg1", "arg 2", "arg 3", "arg4"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "echo command with escaped quotes", | ||
| input: `echo "Hello, \"World!\""`, | ||
| expected: []string{"echo", "Hello, \"World!\""}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "grep command with quoted search term", | ||
| input: `grep -r "search term" /path/to/dir`, | ||
| expected: []string{"grep", "-r", "search term", "/path/to/dir"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "ls command with quoted filename", | ||
| input: `ls -la "file with spaces.txt"`, | ||
| expected: []string{"ls", "-la", "file with spaces.txt"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "empty string", | ||
| input: ``, | ||
| expected: []string{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "multiple spaces", | ||
| input: ` multiple spaces `, | ||
| expected: []string{"multiple", "spaces"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "with comment string", | ||
| input: `echo "Hello, W#orld!" ${USER} # This is a comment`, | ||
| expected: []string{"echo", "Hello, W#orld!", "${USER}"}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "comment only", | ||
| input: `# this line is just a comment`, | ||
| expected: []string{}, | ||
| wantErr: false, | ||
| }, | ||
| // may cause an error in shlex at python3 | ||
| { | ||
| name: "unclosed double quote", | ||
| input: `"unclosed quote`, | ||
| expected: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "unclosed single quote", | ||
| input: `'unclosed quote`, | ||
| expected: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "mixed unclosed quotes", | ||
| input: `"mixed 'quotes`, | ||
| expected: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "single quote closed with double quote", | ||
| input: `"hello world'`, | ||
| expected: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "double quote closed with single quote", | ||
| input: `'hello world"`, | ||
| expected: nil, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| // execute each test case | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // call the ShlexSplit function | ||
| result, err := ShlexSplit(tc.input) | ||
|
|
||
| // check for expected error | ||
| if tc.wantErr { | ||
| if err == nil { | ||
| t.Errorf("FAIL: Expected error but got none, Expected: %q\n", tc.expected) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if assert.NoError(t, err, "FAIL: Unexpected error %q for input %q", err, tc.input) { | ||
| // check if the result matches the expected output | ||
| assert.Equal(t, tc.expected, result, | ||
| "FAIL: Result mismatch,Input %q, Expected %q, Got: %q\n", | ||
| tc.input, tc.expected, result, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we want
require.Error/require.NoErrorin this loop instead of justassert. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference between
requireandassertfunctions in whether execution continues when a test fails—in this test function, specifying either should produce identical behavior. This is because bothassertvariants cause thet.Run()function to terminate immediately upon failure.https://pkg.go.dev/github.com/stretchr/testify/require
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Let's keep as-is.