Skip to content

Commit d66eba6

Browse files
committed
Extract file handling functionality to separate functions
1 parent 9ca5c67 commit d66eba6

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

coauthors_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
5-
"os"
64
"path/filepath"
75
"testing"
86
)
@@ -39,12 +37,7 @@ func TestStartDoneCoAuthors(t *testing.T) {
3937
start(configuration)
4038
done(configuration)
4139

42-
outputFile := filepath.Join(tempDir, "local", ".git", "SQUASH_MSG")
43-
content, err := os.ReadFile(outputFile)
44-
if err != nil {
45-
failWithFailure(t, fmt.Sprintf("reading file %s failed with %v", outputFile, err), "error")
46-
}
47-
output := string(content)
40+
output := readFile(t, filepath.Join(tempDir, "local", ".git", "SQUASH_MSG"))
4841

4942
// don't include the person running `mob done`
5043
assertOutputNotContains(t, &output, "Co-authored-by: local <[email protected]>")

mob_test.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -805,11 +805,7 @@ func TestStartNextStay_DoNotWriteLastModifiedFileInCommit_WhenFileIsDeleted(t *t
805805
next(configuration)
806806

807807
start(configuration)
808-
file1Path := filepath.Join(workingDir, "file1.txt")
809-
err := os.Remove(file1Path)
810-
if err != nil {
811-
failWithFailure(t, "no error", fmt.Sprintf("error %v occured deleting file %s", err, file1Path))
812-
}
808+
removeFile(t, filepath.Join(workingDir, "file1.txt"))
813809
next(configuration)
814810

815811
assertOnBranch(t, "mob-session")
@@ -826,12 +822,7 @@ func TestStartNextStay_DoNotWriteLastModifiedFileInCommit_WhenFileIsMoved(t *tes
826822

827823
start(configuration)
828824
createDirectory(t, "dir")
829-
oldPath := filepath.Join(workingDir, "file1.txt")
830-
newPath := filepath.Join(workingDir, "dir", "file1.txt")
831-
err := os.Rename(oldPath, newPath)
832-
if err != nil {
833-
failWithFailure(t, "no error", fmt.Sprintf("error %v occured moving %s to %s", err, oldPath, newPath))
834-
}
825+
moveFile(t, filepath.Join(workingDir, "file1.txt"), filepath.Join(workingDir, "dir", "file1.txt"))
835826
next(configuration)
836827

837828
assertOnBranch(t, "mob-session")
@@ -865,12 +856,7 @@ func TestRunOutput(t *testing.T) {
865856
setWorkingDir(tempDir + "/local")
866857
start(configuration)
867858
createFile(t, "file1.txt", "asdf")
868-
outputFile := filepath.Join(tempDir, "local", "file1.txt")
869-
content, err := os.ReadFile(outputFile)
870-
if err != nil {
871-
failWithFailure(t, "no error", fmt.Sprintf("error %v occured reading %s", err, outputFile))
872-
}
873-
output := string(content)
859+
output := readFile(t, filepath.Join(tempDir, "local", "file1.txt"))
874860
assertOutputContains(t, &output, "asdf")
875861
}
876862

@@ -1771,6 +1757,29 @@ func createDirectoryInPath(t *testing.T, path, directory string) (pathToFile str
17711757
return
17721758
}
17731759

1760+
func removeFile(t *testing.T, path string) {
1761+
err := os.Remove(path)
1762+
if err != nil {
1763+
failWithFailure(t, "no error", fmt.Sprintf("error %v occured deleting file %s", err, path))
1764+
}
1765+
}
1766+
1767+
func moveFile(t *testing.T, oldPath string, newPath string) {
1768+
err := os.Rename(oldPath, newPath)
1769+
if err != nil {
1770+
failWithFailure(t, "no error", fmt.Sprintf("error %v occured moving %s to %s", err, oldPath, newPath))
1771+
}
1772+
}
1773+
1774+
func readFile(t *testing.T, path string) string {
1775+
content, err := os.ReadFile(path)
1776+
if err != nil {
1777+
failWithFailure(t, "no error", fmt.Sprintf("reading file %s failed with %v", path, err))
1778+
}
1779+
output := string(content)
1780+
return output
1781+
}
1782+
17741783
func assertOnBranch(t *testing.T, branch string) {
17751784
currentBranch := gitCurrentBranch()
17761785
if currentBranch.Name != branch {

0 commit comments

Comments
 (0)