-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add basic ignore directive, allowing the user to tell anchor to ignore certain packages #38
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e3cb23
feat: add basic ignore directive, allowing the user to tell anchor to…
BradLewis 229f096
feat: add ignore for images
BradLewis 0a1c497
chore: only print that the version was anchored when it actually was
BradLewis 66b4a84
feat: add option for ignore all packages/images in command
BradLewis a631685
chore: add documentation on ignore directive
BradLewis 641c44e
fix: readme toc
BradLewis 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 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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -55,3 +55,128 @@ RUN dpkg --add-architecture %s && apt-get update && apt-get update \ | |||||
t.Errorf("Expected:\n%v\ngot:\n%v", expected, result) | ||||||
} | ||||||
} | ||||||
|
||||||
func TestParseComment(t *testing.T) { | ||||||
cases := []struct { | ||||||
name string | ||||||
entry Entry | ||||||
expectedIgnored []string | ||||||
expectedAll bool | ||||||
}{ | ||||||
{ | ||||||
name: "simple comment", | ||||||
entry: Entry{ | ||||||
Type: EntryComment, | ||||||
Value: "# anchor ignore=curl,wget", | ||||||
}, | ||||||
expectedIgnored: []string{"curl", "wget"}, | ||||||
expectedAll: false, | ||||||
}, | ||||||
{ | ||||||
name: "poorly formatted comment", | ||||||
entry: Entry{ | ||||||
Type: EntryComment, | ||||||
Value: "#anchor ignore =curl, test,wget", | ||||||
}, | ||||||
expectedIgnored: []string{"curl", "test", "wget"}, | ||||||
expectedAll: false, | ||||||
}, | ||||||
{ | ||||||
name: "non anchor comment", | ||||||
entry: Entry{ | ||||||
Type: EntryComment, | ||||||
Value: "# hadolint ignore=DL3008", | ||||||
}, | ||||||
expectedIgnored: []string{}, | ||||||
expectedAll: false, | ||||||
}, | ||||||
{ | ||||||
name: "non anchor ignore comment", | ||||||
entry: Entry{ | ||||||
Type: EntryComment, | ||||||
Value: "# anchor is a tool for anchoring dependencies in dockerfiles", | ||||||
}, | ||||||
expectedIgnored: []string{}, | ||||||
expectedAll: false, | ||||||
}, | ||||||
{ | ||||||
name: "basic ignore all", | ||||||
entry: Entry{ | ||||||
Type: EntryComment, | ||||||
Value: "# anchor ignore", | ||||||
}, | ||||||
expectedIgnored: []string{}, | ||||||
expectedAll: true, | ||||||
}, | ||||||
} | ||||||
for _, tc := range cases { | ||||||
t.Run(tc.name, func(t *testing.T) { | ||||||
actual, all := parseComment(tc.entry) | ||||||
if !reflect.DeepEqual(actual, tc.expectedIgnored) { | ||||||
t.Errorf("Expected %v but got %v", tc.expectedIgnored, actual) | ||||||
} | ||||||
if all != tc.expectedAll { | ||||||
t.Errorf("Expected %v but got %v", tc.expectedAll, all) | ||||||
} | ||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
func TestAppendPackageVersionsWithIgnore(t *testing.T) { | ||||||
file := `# hadolint ignore=DL3008 | ||||||
# anchor ignore=curl | ||||||
RUN apt-get update \ | ||||||
&& apt-get install \ | ||||||
--no-install-recommends -y \ | ||||||
# We just need curl and wget | ||||||
curl wget \ | ||||||
&& rm -rf /var/lib/apt/lists/* \ | ||||||
&& apt-get clean` | ||||||
input := strings.NewReader(file) | ||||||
nodes := Parse(input) | ||||||
architecture := "amd64" | ||||||
|
||||||
packageMap := map[string]string{ | ||||||
"curl": "7.68.0", | ||||||
"wget": "1.20.3", | ||||||
} | ||||||
|
||||||
expected := fmt.Sprintf(`# hadolint ignore=DL3008 | ||||||
# anchor ignore=curl | ||||||
RUN dpkg --add-architecture %s && apt-get update && apt-get update \ | ||||||
&& apt-get install \ | ||||||
--no-install-recommends -y \ | ||||||
# We just need curl and wget | ||||||
curl wget=%s \ | ||||||
&& rm -rf /var/lib/apt/lists/* \ | ||||||
&& apt-get clean | ||||||
`, architecture, packageMap["wget"]) | ||||||
|
||||||
node := nodes[0] | ||||||
appendPackageVersions(&node, packageMap, architecture) | ||||||
nodes[0] = node | ||||||
|
||||||
w := &strings.Builder{} | ||||||
nodes.Write(w) | ||||||
result := w.String() | ||||||
if result != expected { | ||||||
t.Errorf("Expected:\n%v\ngot:\n%v", expected, result) | ||||||
} | ||||||
} | ||||||
|
||||||
func TestImageIgnore(t *testing.T) { | ||||||
file := `# hadolint ignore=DL3008 | ||||||
# anchor ignore=golang:1.22-bookworm | ||||||
FROM golang:1.22-bookworm as builder | ||||||
` | ||||||
|
||||||
input := strings.NewReader(file) | ||||||
nodes := Parse(input) | ||||||
image, err := processFromCommand(&nodes[0]) | ||||||
if err != nil { | ||||||
t.Errorf("Expected no error but got %v", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
if image != "golang:1.22-bookworm" { | ||||||
t.Errorf("Expected golang:1.22-bookworm but got %v", image) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this be a
Suggested change
|
||||||
} | ||||||
} |
This file contains 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.
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.
Got GPT-4o to "refactor" this and what it claims (I don't know about readability compared to what you have though)—I'll leave it up to you:
Changes Made:
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.
Turns out this was just wrong lol but thanks chatgpt