Skip to content

Commit

Permalink
Merge pull request #3 from ZupIT/hotfix/fixing-panic-index-out-of-range
Browse files Browse the repository at this point in the history
Fixing panic when get endOfPreviousLine and broken with out of range [-1]
  • Loading branch information
wiliansilvazup authored Sep 17, 2020
2 parents 9e3f93c + 3e90fc1 commit 6f4f212
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
40 changes: 37 additions & 3 deletions text/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int)

// now we access the textual index in the slice to ge the column
endOfCurrentLineInTheFile := textfile.newlineEndingIndexes[endOfCurrentLine]
column = (findingIndex - 1) - endOfCurrentLineInTheFile
if findingIndex == 0 {
column = endOfCurrentLineInTheFile
} else {
column = (findingIndex - 1) - endOfCurrentLineInTheFile
}
}

return
Expand All @@ -116,10 +120,13 @@ func (textfile TextFile) ExtractSample(findingIndex int) string {
lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes)

if lineIndex < len(textfile.newlineEndingIndexes) {
endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex-1]
endOfPreviousLine := 0
if lineIndex > 0 {
endOfPreviousLine = textfile.newlineEndingIndexes[lineIndex - 1] + 1
}
endOfCurrentLine := textfile.newlineEndingIndexes[lineIndex]

lineContent := textfile.RawString[endOfPreviousLine+1 : endOfCurrentLine]
lineContent := textfile.RawString[endOfPreviousLine : endOfCurrentLine]

return strings.TrimSpace(lineContent)
}
Expand Down Expand Up @@ -157,6 +164,33 @@ func LoadDirIntoSingleUnit(path string, extensionsAccept []string) (TextUnit, er
return unit, err
}

// The Param extensionAccept is an filter to check if you need get textUnit for file with this extesion
// Example: []string{".java"}
// If an item of slice contains is equal the "**" it's will accept all extensions
// Example: []string{"**"}
func LoadDirIntoMultiUnit(path string, maxFilesPerTextUnit int, extensionsAccept []string) ([]TextUnit, error) {
units := []TextUnit{
TextUnit{},
}
lastIndexToAdd := 0
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
textFile, err := validateAndGetTextFileByPath(path, extensionsAccept)
if err != nil || textFile == nil {
return err
}
units[lastIndexToAdd].Files = append(units[lastIndexToAdd].Files, *textFile)
if len(units[lastIndexToAdd].Files) >= maxFilesPerTextUnit {
units = append(units, TextUnit{})
lastIndexToAdd++
}
return nil
})
return units, err
}

func validateAndGetTextFileByPath(path string, extensionsAccept []string) (*TextFile, error) {
if checkIfEnableExtension(path, extensionsAccept) {
file, err := getTextFileByPath(path)
Expand Down
11 changes: 11 additions & 0 deletions text/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ func TestTextFiles_GetAllFilesUnits(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 9, len(textUnit.Files))
})
t.Run("Should return multi unit with 4 textFiles and max of 3 files per textFile when get any files", func(t *testing.T) {
path := "./samples"
path, err := filepath.Abs(path)
assert.NoError(t, err)
textUnit, err := LoadDirIntoMultiUnit(path, 3, []string{"**"})
assert.NoError(t, err)
assert.Equal(t, 4, len(textUnit))
for _, item := range textUnit {
assert.LessOrEqual(t, len(item.Files), 3)
}
})
t.Run("Should return unit with tree files when get go files", func(t *testing.T) {
path := "./samples"
path, err := filepath.Abs(path)
Expand Down

0 comments on commit 6f4f212

Please sign in to comment.