Skip to content
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

Improve code coverage for uiconv package #3

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/anonymizer/app/uiconv/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Reader struct {
func NewReader(capturedFile string, logger *zap.Logger) (*Reader, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
return nil, fmt.Errorf("cannot get working directory: %w", err)
}
logger.Sugar().Infof("Current working dir is %s", wd)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a debugging left-over, I don't see wd actually being used, if so it's best to delete L40-44


Expand Down
23 changes: 21 additions & 2 deletions cmd/anonymizer/app/uiconv/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package uiconv

import (
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -37,15 +38,17 @@ func TestReader_TraceSuccess(t *testing.T) {
assert.Equal(t, 1, r.spansRead)
assert.Equal(t, false, r.eofReached)

r.spansRead = 999

s2, err := r.NextSpan()
require.NoError(t, err)
assert.Equal(t, "471418097747d04a", s2.OperationName)
assert.Equal(t, 2, r.spansRead)
assert.Equal(t, 1000, r.spansRead)
assert.Equal(t, true, r.eofReached)

_, err = r.NextSpan()
require.Equal(t, io.EOF, err)
assert.Equal(t, 2, r.spansRead)
assert.Equal(t, 1000, r.spansRead)
assert.Equal(t, true, r.eofReached)
}

Expand Down Expand Up @@ -99,3 +102,19 @@ func TestReader_TraceInvalidJson(t *testing.T) {
assert.Equal(t, 0, r.spansRead)
assert.Equal(t, true, r.eofReached)
}

func TestReader_GetWDError(t *testing.T) {
inputFile := "fixtures/trace_non_existent.json"

wd, err := os.Getwd()
require.NoError(t, err)
err = os.Chmod(wd, 0000)
require.NoError(t, err)
defer os.Chmod(wd, 0755)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clever, but it's conceivable that the tests might run on a read-only source dir where chmod won't work. If we can get rid of Getwd per my comment above, let's do that instead.


_, err = NewReader(
inputFile,
zap.NewNop(),
)
require.Contains(t, err.Error(), "cannot get working directory")
}