Skip to content

Commit

Permalink
Improve input iteration by adding more tests. (#336)
Browse files Browse the repository at this point in the history
* 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
calebbrown authored Feb 27, 2023
1 parent 1e7ad38 commit aedb63b
Show file tree
Hide file tree
Showing 16 changed files with 598 additions and 169 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ permissions: read-all

jobs:
run-tests:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568
with:
go-version: 1.19
- name: Run tests
run: make test
run: make test/unit
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run-linter:
Expand All @@ -29,3 +32,11 @@ jobs:
go-version: 1.19
- name: golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5
check-scorecard-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c
- name: Run tests
run: make test/scorecard-version
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
139 changes: 0 additions & 139 deletions cmd/criticality_score/input.go

This file was deleted.

30 changes: 30 additions & 0 deletions cmd/criticality_score/inputiter/err_windows.go
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)
}
}
95 changes: 95 additions & 0 deletions cmd/criticality_score/inputiter/iterator.go
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
}
Loading

0 comments on commit aedb63b

Please sign in to comment.