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

Add unit tests for /pkg/rtc #2163

Closed
wants to merge 3 commits into from
Closed

Add unit tests for /pkg/rtc #2163

wants to merge 3 commits into from

Conversation

depate
Copy link
Contributor

@depate depate commented Nov 17, 2021

No description provided.

@codecov
Copy link

codecov bot commented Nov 19, 2021

Codecov Report

Merging #2163 (2822acc) into master (b4ba9d2) will decrease coverage by 0.43%.
The diff coverage is 100.00%.

❗ Current head 2822acc differs from pull request most recent head 0dc9067. Consider uploading reports for the commit 0dc9067 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2163      +/-   ##
==========================================
- Coverage   60.31%   59.88%   -0.44%     
==========================================
  Files         399      398       -1     
  Lines       40606    40671      +65     
==========================================
- Hits        24490    24354     -136     
- Misses      15152    15358     +206     
+ Partials      964      959       -5     
Impacted Files Coverage Δ
pkg/rtc/rtc.go 100.00% <ø> (+100.00%) ⬆️
pkg/rtc/rtc_linux.go 100.00% <100.00%> (+100.00%) ⬆️
cmds/core/dd/dd.go 5.24% <0.00%> (-59.56%) ⬇️
pkg/cmos/cmos.go 0.00% <0.00%> (ø)
cmds/core/id/id.go 5.66% <0.00%> (ø)
pkg/acpi/tables.go 0.00% <0.00%> (ø)
pkg/cpio/fs_unix.go 28.57% <0.00%> (ø)
pkg/ldd/ldd_unix.go 60.00% <0.00%> (ø)
cmds/exp/cbmem/io.go 100.00% <0.00%> (ø)
pkg/termios/sgtty.go 54.28% <0.00%> (ø)
... and 30 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b4ba9d2...0dc9067. Read the comment docs.

Copy link
Member

@rminnich rminnich left a comment

Choose a reason for hiding this comment

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

A few comments, see what you think. The only substantive one is that it would be best to remove your temp files at the end of the test via defer

rtc, err := OpenRTC()
if err != nil {
t.Error(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

not strictly needed but it is common to just put a
defer rtc.Close()
at a place like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wasn't sure if the defer would have run on the end of t.Run() or the end of the loop where all the t.Run() are executed in so I went for the safe way and did a rtc.Close() a few lines further down where I would be sure I don't need the rtc anymore.

Copy link
Member

Choose a reason for hiding this comment

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

ah, ok. I'd suggest putting a comment in about this, so that in three years someone else is as confused as I am :-)

}
}
rtc.Close()
err = os.Remove(devs[0])
Copy link
Member

Choose a reason for hiding this comment

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

A common pattern is
if err := os.Remove(...); err != nil {
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I saw that comment a few times now and I wondered what the reason behind that pattern is. Don't get me wrong, I know it works but why deviate from a uniform pattern only in a few random occasions? Maybe you know something I don't so would be good to know the reason for that pattern.

Copy link
Member

Choose a reason for hiding this comment

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

The reason for the pattern is to hold the err value for that one particular test. The err variable is captured in that one if err := block. It avoids creating a variable outside the scope of that one particular function call, which can be useful for avoiding accidents (which have happened in practice) either caused by assigning to an err value that already existed; or by creating an err value that causes trouble later. It's a very common pattern, and it's a bit jarring not to see it used, as on lines 43 and 52; if it is ok it would be nice to use it. There is a writeup from the early go days about why to use it but ... I can't find it!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah that makes sense! I'll change it up then, thanks for the explanation.

t.Error(err)
}
devs = []string{f.Name()}
f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

it's common to have a
defer os.Remove(f.Name())
so we don't litter /tmp with these files.
This is a failing of Unix and its share /tmp, but it's all we have ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, instead of running defer I manually called os.Remove on it further down in the form of os.Remove(devs[0]) as the devs array only holds a single string that is f.Name()

Copy link
Contributor

Choose a reason for hiding this comment

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

For sure this works here. Using defer is just good practice. For this code it does not matter.

Copy link
Member

@rminnich rminnich left a comment

Choose a reason for hiding this comment

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

I added some comments. Thanks.

rtc, err := OpenRTC()
if err != nil {
t.Error(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

ah, ok. I'd suggest putting a comment in about this, so that in three years someone else is as confused as I am :-)

}
}
rtc.Close()
err = os.Remove(devs[0])
Copy link
Member

Choose a reason for hiding this comment

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

The reason for the pattern is to hold the err value for that one particular test. The err variable is captured in that one if err := block. It avoids creating a variable outside the scope of that one particular function call, which can be useful for avoiding accidents (which have happened in practice) either caused by assigning to an err value that already existed; or by creating an err value that causes trouble later. It's a very common pattern, and it's a bit jarring not to see it used, as on lines 43 and 52; if it is ok it would be nice to use it. There is a writeup from the early go days about why to use it but ... I can't find it!

Signed-off-by: Marvin Drees <marvin.drees@9elements.com>
Some permission tests that work locally
are failing in the CI for unknown reasons.
Those are now disabled there.

Signed-off-by: Marvin Drees <marvin.drees@9elements.com>
Signed-off-by: Marvin Drees <marvin.drees@9elements.com>
@MDr164 MDr164 mentioned this pull request Nov 23, 2021
@MDr164
Copy link
Contributor

MDr164 commented Nov 23, 2021

Close in favor of #2184 because of CI issues.

@MDr164 MDr164 closed this Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants