Repository navigation
refactor: streamline Makefile and add diff functionality with tests #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
086f338
refactor: streamline Makefile and add diff functionality with tests
kazeburo 8c4c517
Update diff.go
kazeburo ac9ed4a
Update diff.go
kazeburo b51bdf6
fix: handle error case for missing command in main function
kazeburo 8a2b0ea
Update diff.go
kazeburo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/cubicdaiya/gonp" | ||
| ) | ||
|
|
||
| func getLines(filename string) ([]string, error) { | ||
| file, err := openRD(filename) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer file.Close() | ||
|
|
||
| scanner := bufio.NewScanner(file) | ||
| scanner.Buffer(make([]byte, 64*1024), 10*1024*1024) | ||
| lines := make([]string, 0) | ||
| for scanner.Scan() { | ||
| lines = append(lines, scanner.Text()) | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
| return lines, nil | ||
| } | ||
|
|
||
| func diff(prev, current string) (string, error) { | ||
| prevLines, err := getLines(prev) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| newLines, err := getLines(current) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| d := gonp.New(prevLines, newLines) | ||
| d.Compose() | ||
|
|
||
| return d.SprintUniHunks(d.UnifiedHunks()), nil | ||
| } | ||
|
|
||
| func buildNoDifferenceMsg(filename string) (string, error) { | ||
| file, err := openRD(filename) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer file.Close() | ||
|
|
||
| fileinfo, err := file.Stat() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| b := make([]byte, 128) | ||
| count, err := file.Read(b) | ||
| if err != nil && err != io.EOF { | ||
| return "", err | ||
| } | ||
|
kazeburo marked this conversation as resolved.
|
||
| o := string(strings.TrimRight(string(b[0:count]), "\r\n")) | ||
| if fileinfo.Size() > 128 { | ||
| return fmt.Sprintf("no difference: ```%s...```", o), nil | ||
| } | ||
| return fmt.Sprintf("no difference: ```%s```", o), nil | ||
| } | ||
|
|
||
| func buildDiffMsg(diff string) string { | ||
| o := diff | ||
| if len(diff) > 512 { | ||
| o = diff[0:512] | ||
| } | ||
| o = strings.TrimRight(o, "\r\n") | ||
|
|
||
| if len(diff) > 512 { | ||
| return fmt.Sprintf("found difference: ```%s...```", o) | ||
| } | ||
| return fmt.Sprintf("found difference: ```%s```", o) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestDiff(t *testing.T) { | ||
| file1, cleanup1 := createTempFile(t, "Hello, World!\n") | ||
| defer cleanup1() | ||
|
|
||
| file2, cleanup2 := createTempFile(t, "Hello, Go!\n") | ||
| defer cleanup2() | ||
|
|
||
| diffResult, err := diff(file1.Name(), file2.Name()) | ||
| if err != nil { | ||
| t.Fatalf("diff failed: %v", err) | ||
| } | ||
| if diffResult == "" { | ||
| t.Errorf("Expected a diff result, got empty string") | ||
| } | ||
| if !strings.Contains(diffResult, "Hello, World!") || !strings.Contains(diffResult, "Hello, Go!") { | ||
| t.Errorf("Diff result does not contain expected content: %q", diffResult) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildNoDifferenceMsg(t *testing.T) { | ||
| shortMsg := "This is a test message." | ||
| file, cleanup := createTempFile(t, shortMsg+"\n") | ||
| defer cleanup() | ||
| expected := "no difference: ```" + shortMsg + "```" | ||
| result, err := buildNoDifferenceMsg(file.Name()) | ||
| if err != nil { | ||
| t.Fatalf("buildNoDifferenceMsg failed: %v", err) | ||
| } | ||
| if result != expected { | ||
| t.Errorf("Expected %q, got %q", expected, result) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildNoDifferenceMsgLargeResult(t *testing.T) { | ||
| largeMsg := strings.Repeat("A", 600) | ||
| file, cleanup := createTempFile(t, largeMsg+"\n") | ||
| defer cleanup() | ||
|
|
||
| expected := "no difference: ```" + largeMsg[:128] + "...```" | ||
| result, err := buildNoDifferenceMsg(file.Name()) | ||
| if err != nil { | ||
| t.Fatalf("buildNoDifferenceMsg failed: %v", err) | ||
| } | ||
| if result != expected { | ||
| t.Errorf("Expected %q, got %q", expected, result) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildDiffMsg(t *testing.T) { | ||
| diff := "This is a test diff message.\nThis is the second line of the diff.\n" | ||
| expected := "found difference: ```This is a test diff message.\nThis is the second line of the diff.```" | ||
| result := buildDiffMsg(diff) | ||
| if result != expected { | ||
| t.Errorf("Expected %q, got %q", expected, result) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildDiffMsgLargeResult(t *testing.T) { | ||
| diff := strings.Repeat("A", 600) | ||
| expected := "found difference: ```" + diff[:512] + "...```" | ||
| result := buildDiffMsg(diff) | ||
| if result != expected { | ||
| t.Errorf("Expected %q, got %q", expected, result) | ||
| } | ||
| } | ||
|
|
||
| func TestDiffNoDifference(t *testing.T) { | ||
| file1, cleanup1 := createTempFile(t, "Hello, World!\n") | ||
| defer cleanup1() | ||
|
|
||
| file2, cleanup2 := createTempFile(t, "Hello, World!\n") | ||
| defer cleanup2() | ||
|
|
||
| diffResult, err := diff(file1.Name(), file2.Name()) | ||
| if err != nil { | ||
| t.Fatalf("diff failed: %v", err) | ||
| } | ||
| if diffResult != "" { | ||
| t.Errorf("Expected no diff result, got: %q", diffResult) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.