Skip to content

Commit

Permalink
Port detection Optimization for Go Projects (#38)
Browse files Browse the repository at this point in the history
* turn on default linters and fix warnings/errors as a result

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* cleanup forgotten commented out block

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* potential increase in code cov for resp body closing

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* fix lint error in test case

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* revert redundant code coverage changes

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* update defer statements and add test cases for them

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* add test cases for detector.go

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* update excluded folders for mocks and migrations

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* update unit tests for new go excluded folders

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* filter out test files for go

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* make changes more concise

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* update unit tests for optimization changes

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* update port detection docs

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* remove commented out block

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* revert linter change from other branch

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* revert defer statement changes from other branch

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* revert detector.go test changes from other branch

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* update port detection docs with suggested change

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

* add unit test for new function

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>

---------

Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>
  • Loading branch information
Jdubrick authored Dec 19, 2023
1 parent 393ca66 commit 8f277a2
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 18 deletions.
2 changes: 2 additions & 0 deletions docs/proposals/port_detection_optimizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The main idea of the proposal will be to introduce 2 different optimizations reg
* [Add specific port detection paths to each detector](#detector-port-detection-paths).
* [Define no-port-detection CLI arg](#define-no-port-detection-cli-arg).

Note that, due to performance reasons, the port detection process for Go files/projects ignores any testing file with the suffix `_test.go`. Additionally, it also ignores `mocks` and `migrations` labelled directories.

## Detector Port Detection Paths
As a first step for better performance during port detection we can define certain paths and files for port detection and each detector. So, upon port detection process the detector will only check for configured ports inside these paths. We can apply this to all of the detectors in order to have a consistent file read process for each `DoPortDetection` function.

Expand Down
30 changes: 17 additions & 13 deletions pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,23 +568,27 @@ func GetAnyApplicationFilePathExactMatch(root string, propsFiles []model.Applica
func GenerateApplicationFileFromFilters(files []string, path string, suffix string, ctx *context.Context) []model.ApplicationFileInfo {
applicationFileInfos := []model.ApplicationFileInfo{}
for _, file := range files {
if strings.HasSuffix(file, suffix) {
cleanPath := filepath.Clean(file)
filename := filepath.Base(cleanPath)
tmpDir := strings.ReplaceAll(file, path, "")
dir := strings.ReplaceAll(tmpDir, filename, "")
appFileInfo := model.ApplicationFileInfo{
Context: ctx,
Root: path,
Dir: dir,
File: filename,
}
applicationFileInfos = append(applicationFileInfos, appFileInfo)
}
if strings.HasSuffix(file, suffix) && !strings.HasSuffix(file, "_test.go"){
applicationFileInfos = append(applicationFileInfos, createAppFileInfo(file, path, ctx))
}
}
return applicationFileInfos
}

func createAppFileInfo(file string, path string, ctx *context.Context) model.ApplicationFileInfo {
cleanPath := filepath.Clean(file)
filename := filepath.Base(cleanPath)
tmpDir := strings.ReplaceAll(file, path, "")
dir := strings.ReplaceAll(tmpDir, filename, "")
appFileInfo := model.ApplicationFileInfo{
Context: ctx,
Root: path,
Dir: dir,
File: filename,
}
return appFileInfo
}

// GetApplicationFileContents returns a slice of strings for all file contents found for a given
// slice of ApplicationFileInfo.
func GetApplicationFileContents(appFileInfos []model.ApplicationFileInfo) ([]string, error) {
Expand Down
94 changes: 92 additions & 2 deletions pkg/utils/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ func TestGenerateApplicationFileFromFilters(t *testing.T) {
want []model.ApplicationFileInfo
}{
{
name: "case 1: found ApplicationFileInfo",
name: "case 1: found ApplicationFileInfo for Go",
args: args{
files: []string{
"../../resources/projects/echo/main.go",
Expand All @@ -1658,7 +1658,34 @@ func TestGenerateApplicationFileFromFilters(t *testing.T) {
},
},
{
name: "case 2: not found ApplicationFileInfo",
name: "case 2: found ApplicationFileInfo for Js",
args: args{
files: []string{
"../../resources/projects/reactjs/src/index.js",
"../../resources/projects/reactjs/src/config.js",
"../../resources/projects/reactjs/src/index.scss",
},
path: "../../resources/projects/reactjs/src/",
suffix: ".js",
ctx: &ctx,
},
want: []model.ApplicationFileInfo{
{
Dir: "",
File: "index.js",
Root: "../../resources/projects/reactjs/src/",
Context: &ctx,
},
{
Dir: "",
File: "config.js",
Root: "../../resources/projects/reactjs/src/",
Context: &ctx,
},
},
},
{
name: "case 3: not found ApplicationFileInfo for Go",
args: args{
files: []string{
"../../resources/projects/echo/go.mod",
Expand All @@ -1670,6 +1697,39 @@ func TestGenerateApplicationFileFromFilters(t *testing.T) {
},
want: []model.ApplicationFileInfo{},
},
{
name: "case 4: not found ApplicationFileInfo for Js",
args: args{
files: []string{
"../../resources/projects/reactjs/src/index.scss",
},
path: "../../resources/projects/reactjs/src/",
suffix: ".js",
ctx: &ctx,
},
want: []model.ApplicationFileInfo{},
},
{
name: "case 5: found ApplicationFileInfo for Go excluding test files",
args: args{
files: []string{
"../../resources/projects/golang-gin-app/common/unit_test.go",
"../../resources/projects/golang-gin-app/doc.go",
"../../resources/projects/golang-gin-app/go.sum",
},
path: "../../resources/projects/golang-gin-app/",
suffix: ".go",
ctx: &ctx,
},
want: []model.ApplicationFileInfo{
{
Dir: "",
File: "doc.go",
Root: "../../resources/projects/golang-gin-app/",
Context: &ctx,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -2059,4 +2119,34 @@ func TestCloseFile(t *testing.T){
assert.EqualValues(t, tt.expectedOut, string(out))
})
}
}
func TestCreateAppFileInfo(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
file string
path string
ctx *context.Context
want model.ApplicationFileInfo
}{
{
name: "case 1: created app file info",
file: "../../resources/projects/echo/main.go",
path: "../../resources/projects/echo/",
ctx: &ctx,
want: model.ApplicationFileInfo{
Dir: "",
File: "main.go",
Root: "../../resources/projects/echo/",
Context: &ctx,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T){
appFileInfo := createAppFileInfo(tt.file, tt.path, tt.ctx)
assert.EqualValues(t, appFileInfo, tt.want)
})
}
}
6 changes: 3 additions & 3 deletions pkg/utils/langfiles/languages_file_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestGetLanguageByName(t *testing.T) {
},
{
name: "Go",
expectedItem: LanguageItem{Name: "Go", Aliases: []string{"golang"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"go.mod"}, ExcludeFolders: []string{"vendor"}, Component: true, disabled: false},
expectedItem: LanguageItem{Name: "Go", Aliases: []string{"golang"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"go.mod"}, ExcludeFolders: []string{"vendor", "mocks", "migrations"}, Component: true, disabled: false},
expectedErr: nil,
},
{
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestGetLanguageByAlias(t *testing.T) {
{
name: "Go",
alias: "golang",
expectedItem: LanguageItem{Name: "Go", Aliases: []string{"golang"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"go.mod"}, ExcludeFolders: []string{"vendor"}, Component: true, disabled: false},
expectedItem: LanguageItem{Name: "Go", Aliases: []string{"golang"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"go.mod"}, ExcludeFolders: []string{"vendor", "mocks", "migrations"}, Component: true, disabled: false},
expectedErr: nil,
},
{
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestGetLanguageByAlias(t *testing.T) {

func TestGetExcludedFolders(t *testing.T) {
languageFile := Get()
expectedFolders := []string{"node_modules", "vendor"}
expectedFolders := []string{"node_modules", "vendor", "mocks", "migrations"}
excludedFolders := languageFile.GetExcludedFolders()
assert.ElementsMatch(t, excludedFolders, expectedFolders)
}
2 changes: 2 additions & 0 deletions pkg/utils/langfiles/resources/languages-customization.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ GCC Machine Description:
Go:
exclude_folders:
- "vendor"
- "mocks"
- "migrations"
configuration_files:
- "go.mod"
component: true
Expand Down

0 comments on commit 8f277a2

Please sign in to comment.