-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Move scaffold machinery to its own internal package #1339
Move scaffold machinery to its own internal package #1339
Conversation
Hi @Adirio. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
15bc09e
to
42ba9f1
Compare
4dba071
to
d02ee65
Compare
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.
/lgtm
Refactor makes a lot of sense.
d02ee65
to
885d82e
Compare
6050e7c
to
d43311f
Compare
@estroz I rebased so Also, could you add |
This comment has been minimized.
This comment has been minimized.
/ok-to-test |
/approve rebase and get one of the reviewers to re-apply the lgtm, and this should be good |
@camilamacedo86 Had to rename them back to mock because golang didnt allow to have test files without test and these two are artifacts for test, but not tests themselves. Maybe we can rename them to test when we add tests. You can see the error here. |
Hi @Adirio,
I do not think that the error faced is because of this. See here in a personal project that I have a file called mocks_test.go which has no tests. The problem is that the mock files are implementing funcs and not just has mock data.
I have a few questions:
|
Yeah, right, the error may be because we are importing from
They are not used yet. Tests were removed in #1343 as requested in #1342 as they were doing the same as the golden test.
These mocks are not intended to test |
Hi @Adirio, Thank you for the clarifications. Following some comments inline.
I do not think that this should be the correct way to move forward. |
3dc2609
to
f5ed2e6
Compare
/test pull-kubebuilder-e2e-k8s-1-15-3 |
f5ed2e6
to
0083119
Compare
0083119
to
68d5589
Compare
Coverage:
|
2baa6cc
to
f96d453
Compare
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
@@ -0,0 +1,275 @@ | |||
/* | |||
Copyright 2020 The Kubernetes Authors. |
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.
Do not show strange the need to implement tests for the mock tests?
I mean, because we are not using the "real" implementations and structures and we are here re-written them we need also create tests for the tests. So, IHMO the mock files are not required at all and we should use the "real" structures and implementations in the tests by mocking the data on them.
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.
Because these are unitary tests. Unitary tests only test one thing, as oposed to end-to-end (e2e) tests, which test the whole process.
scaffold_test.go
tests the machinery
package. It doesn't test the underlying file system. It doesn't test if the files are created. It just tests the logic in machinery
. In order to do so we need to create a fake underlying filesystem. That's our mockFileSystem.
A mock is not a test, it is a utility that other tests will use. In this case scaffold_test.go
uses mockFileSystem
to abstract the file system. By doing so, errors in FileSystem
will not be caught by scaffold_test.go
as he is not testing that package. They will be caught by filesystem_test.go
.
Does a mock need to be tested? Well, they are no tests themselves so, testing mocks is never a bad thing to do. Most of the things (if not all of them) tested in mock_test.go
would also be caught by scaffold_test.go
, but goveralls
doesn't mark as covered statements from other packages. So, despite I was claiming 79.7% was covered, the Coverage report in Travis was showing a ~35% instead. Most of the tests are really trivial, and it took 0.024s to test it, so there is no need to worry about overlapping tests.
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.
Then it won't be an unitary test. It would be an e2e test. Because if we use fileSystem
we would be creating the file in the disk. We are already doing that with the golden test.
And furthermore, if we use fileSystem
we would need to create functions to read the data and check it is the same.
}) | ||
|
||
// fakePlugin is used to mock a model.Plugin in order to test Scaffold | ||
type fakePlugin struct { |
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.
Why we need the fakePlugin? Why not use the Plugin structure with mock data in the tests?
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.
Plugin
is an interface. fakePlugin
implements this interface. There is no Plugin structure nor data about it.
} | ||
|
||
// fakeFile is used to mock a file.File in order to test Scaffold | ||
type fakeFile struct { |
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.
Why we need the fakeFile? Why not use the eFile structure with mock data in the tests?
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.
Same as Plugin
, File
is a interface. fakeFile
implements this interface allowing us to define the data or the errors that it will yield, so that we can test the machinery
package.
|
||
// MockExistsError makes FileSystem.Exists return err | ||
func MockExistsError(err error) MockOptions { | ||
return func(fs *mockFileSystem) { |
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.
Why we do not use the funcs in pkg/scaffold/internal/filesystem/errors.go
instead of re-writen them to check the errors?
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.
The functions in pkg/scaffold/internal/filesystem/errors.go
check if an error is of a certain type. This functions make the mockFileSystem
return an error at that step.
When you call FileSystem.Exists
an error is returned (it may be nil
).
fileSysten
implements theFileSystem
interface. If an error happens, for example you are not allowed to read in that directory so you cna't check if it exists,fileSystem.Exists
will return that error.mockFileSystem
implements theFileSystem
interface too. When you callmockFileSystem.Exists
the error returned is the one you provided withMockCreateFileError
.
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.
Hi @Adirio,
Because these are unitary tests. Unitary tests only test one thing, as oposed to end-to-end (e2e) tests, which test the whole process.
IHMO: We do not need to test and verify other places and structures. However, we do need to create/duplicate the "real" implementation to do these mock helpers. IHMO we should use the real "structures" and methods implemented to mock the data instead of that.
However, I think we can re-check it in the future since it in POV is not a big deal at all too. Also, since @DirectXMan12 @estroz already gave their OK for this PR as well.
Following the :
/lgtm
For we are able to merge this one. 👍
Description
Scaffolding machinery is spread in the same subpackage that scaffolders. Moving it to an internal package not only helps in tidying up the code, but reduces the attack surface as the machinery can not be used from outside
pkg/scaffold
.The mock version of the scaffold machinery has also received a mayor overhaul.
Motivation
This PR is scoped under #1218.