-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve input iteration by adding more tests. (#336)
* Improve input iteration by adding more tests. Signed-off-by: Caleb Brown <calebbrown@google.com> * Check the errors from inputiter.New during tests. Signed-off-by: Caleb Brown <calebbrown@google.com> * Figure out what type the Windows file error is. Signed-off-by: Caleb Brown <calebbrown@google.com> * Add windows-specific code to handle invalid filename errors. Signed-off-by: Caleb Brown <calebbrown@google.com> * Add missing errors import Signed-off-by: Caleb Brown <calebbrown@google.com> * Remove ErrInvalid as it is not normally returned. Signed-off-by: Caleb Brown <calebbrown@google.com> * Add copyright notices. Signed-off-by: Caleb Brown <calebbrown@google.com> * Close files opened in tests so they can be cleaned up properly. Signed-off-by: Caleb Brown <calebbrown@google.com> * After creating the filename, move to a different dir. Signed-off-by: Caleb Brown <calebbrown@google.com> * Skip tests that fail on Windows. Signed-off-by: Caleb Brown <calebbrown@google.com> * Minor clean-up and test improvements. Signed-off-by: Caleb Brown <calebbrown@google.com> * Add some comments to clarify why we Getwd + Chdir Signed-off-by: Caleb Brown <calebbrown@google.com> * Split unit-test and scorecard-version check into separate workflow jobs. Signed-off-by: Caleb Brown <calebbrown@google.com> --------- Signed-off-by: Caleb Brown <calebbrown@google.com>
- Loading branch information
1 parent
1e7ad38
commit aedb63b
Showing
16 changed files
with
598 additions
and
169 deletions.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2022 Criticality Score Authors | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
//go:build windows | ||
|
||
package inputiter | ||
|
||
import ( | ||
"errors" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func init() { | ||
// Windows can return an additional error number when there is a failure. | ||
osErrorWithFilename = func(err error) bool { | ||
return errors.Is(err, windows.ERROR_INVALID_NAME) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2022 Criticality Score Authors | ||
// | ||
// 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 | ||
// | ||
// https://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 inputiter | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
) | ||
|
||
// Iter is a simple interface for iterating across a list of items. | ||
// | ||
// This interface is modeled on the bufio.Scanner behavior. | ||
type Iter[T any] interface { | ||
// Item returns the current item in the iterator. | ||
// | ||
// Next() must be called before calling Item(). | ||
Item() T | ||
|
||
// Next advances the iterator to the next item and returns true if there is | ||
// an item to consume, and false if the end of the input has been reached, | ||
// or there has been an error. | ||
// | ||
// Next must be called before each call to Item. | ||
Next() bool | ||
|
||
// Err returns any error produced while iterating. | ||
Err() error | ||
} | ||
|
||
// IterCloser is an iter, but also embeds the io.Closer interface, so it can be | ||
// used to wrap a file for iterating through. | ||
type IterCloser[T any] interface { | ||
Iter[T] | ||
io.Closer | ||
} | ||
|
||
// scannerIter implements Iter[string] using a bufio.Scanner to iterate through | ||
// lines in a file. | ||
type scannerIter struct { | ||
c io.Closer | ||
scanner *bufio.Scanner | ||
} | ||
|
||
func (i *scannerIter) Item() string { | ||
return i.scanner.Text() | ||
} | ||
|
||
func (i *scannerIter) Next() bool { | ||
return i.scanner.Scan() | ||
} | ||
|
||
func (i *scannerIter) Err() error { | ||
return i.scanner.Err() | ||
} | ||
|
||
func (i *scannerIter) Close() error { | ||
return i.c.Close() | ||
} | ||
|
||
// sliceIter implements iter using a slice for iterating. | ||
type sliceIter[T any] struct { | ||
values []T | ||
next int | ||
} | ||
|
||
func (i *sliceIter[T]) Item() T { | ||
return i.values[i.next-1] | ||
} | ||
|
||
func (i *sliceIter[T]) Next() bool { | ||
if i.next <= len(i.values) { | ||
i.next++ | ||
} | ||
return i.next <= len(i.values) | ||
} | ||
|
||
func (i *sliceIter[T]) Err() error { | ||
return nil | ||
} | ||
|
||
func (i *sliceIter[T]) Close() error { | ||
return nil | ||
} |
Oops, something went wrong.