|
9 | 9 | "net/http" |
10 | 10 | "net/url" |
11 | 11 | "testing" |
| 12 | + "time" |
12 | 13 |
|
13 | 14 | auth_model "code.gitea.io/gitea/models/auth" |
14 | 15 | repo_model "code.gitea.io/gitea/models/repo" |
@@ -81,47 +82,54 @@ func TestAPIChangeFiles(t *testing.T) { |
81 | 82 | "master", // Branch |
82 | 83 | "", // Empty branch |
83 | 84 | } { |
84 | | - fileID++ |
85 | | - createTreePath := fmt.Sprintf("new/file%d.txt", fileID) |
86 | | - updateTreePath := fmt.Sprintf("update/file%d.txt", fileID) |
87 | | - deleteTreePath := fmt.Sprintf("delete/file%d.txt", fileID) |
88 | | - createFile(user2, repo1, updateTreePath) |
89 | | - createFile(user2, repo1, deleteTreePath) |
90 | | - changeFilesOptions := getChangeFilesOptions() |
91 | | - changeFilesOptions.BranchName = branch |
92 | | - changeFilesOptions.Files[0].Path = createTreePath |
93 | | - changeFilesOptions.Files[1].Path = updateTreePath |
94 | | - changeFilesOptions.Files[2].Path = deleteTreePath |
95 | | - req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo1.Name), &changeFilesOptions). |
96 | | - AddTokenAuth(token2) |
97 | | - resp := MakeRequest(t, req, http.StatusCreated) |
98 | | - gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1) |
99 | | - commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName) |
100 | | - createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath) |
101 | | - updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath) |
102 | | - expectedCreateFileResponse := getExpectedFileResponseForCreate(fmt.Sprintf("%v/%v", user2.Name, repo1.Name), commitID, createTreePath, createLasCommit.ID.String(), createLasCommit.Committer.When) |
103 | | - expectedUpdateFileResponse := getExpectedFileResponseForUpdate(commitID, updateTreePath, updateLastCommit.ID.String(), updateLastCommit.Committer.When) |
104 | | - var filesResponse api.FilesResponse |
105 | | - DecodeJSON(t, resp, &filesResponse) |
| 85 | + t.Run(fmt.Sprintf("Branch %s", branch), func(t *testing.T) { |
| 86 | + fileID++ |
| 87 | + createTreePath := fmt.Sprintf("new/file%d.txt", fileID) |
| 88 | + updateTreePath := fmt.Sprintf("update/file%d.txt", fileID) |
| 89 | + deleteTreePath := fmt.Sprintf("delete/file%d.txt", fileID) |
| 90 | + createFile(user2, repo1, updateTreePath) |
| 91 | + createFile(user2, repo1, deleteTreePath) |
| 92 | + changeFilesOptions := getChangeFilesOptions() |
| 93 | + changeFilesOptions.BranchName = branch |
| 94 | + changeFilesOptions.Files[0].Path = createTreePath |
| 95 | + changeFilesOptions.Files[1].Path = updateTreePath |
| 96 | + changeFilesOptions.Files[2].Path = deleteTreePath |
| 97 | + req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo1.Name), &changeFilesOptions). |
| 98 | + AddTokenAuth(token2) |
| 99 | + resp := MakeRequest(t, req, http.StatusCreated) |
| 100 | + gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1) |
| 101 | + commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName) |
| 102 | + createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath) |
| 103 | + updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath) |
| 104 | + expectedCreateFileResponse := getExpectedFileResponseForCreate(fmt.Sprintf("%v/%v", user2.Name, repo1.Name), commitID, createTreePath, createLasCommit.ID.String(), createLasCommit.Committer.When) |
| 105 | + expectedUpdateFileResponse := getExpectedFileResponseForUpdate(commitID, updateTreePath, updateLastCommit.ID.String(), updateLastCommit.Committer.When) |
| 106 | + var filesResponse api.FilesResponse |
| 107 | + DecodeJSON(t, resp, &filesResponse) |
106 | 108 |
|
107 | | - // check create file |
108 | | - assert.EqualValues(t, expectedCreateFileResponse.Content, filesResponse.Files[0]) |
| 109 | + // FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang, |
| 110 | + // assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be. |
| 111 | + expectedCreateFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedCreateFileResponse.Content.LastCommitWhen.Format(time.RFC3339)) |
| 112 | + expectedUpdateFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedUpdateFileResponse.Content.LastCommitWhen.Format(time.RFC3339)) |
109 | 113 |
|
110 | | - // check update file |
111 | | - assert.EqualValues(t, expectedUpdateFileResponse.Content, filesResponse.Files[1]) |
| 114 | + // check create file |
| 115 | + assert.EqualValues(t, expectedCreateFileResponse.Content, filesResponse.Files[0]) |
112 | 116 |
|
113 | | - // test commit info |
114 | | - assert.EqualValues(t, expectedCreateFileResponse.Commit.SHA, filesResponse.Commit.SHA) |
115 | | - assert.EqualValues(t, expectedCreateFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) |
116 | | - assert.EqualValues(t, expectedCreateFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email) |
117 | | - assert.EqualValues(t, expectedCreateFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name) |
118 | | - assert.EqualValues(t, expectedCreateFileResponse.Commit.Committer.Email, filesResponse.Commit.Committer.Email) |
119 | | - assert.EqualValues(t, expectedCreateFileResponse.Commit.Committer.Name, filesResponse.Commit.Committer.Name) |
| 117 | + // check update file |
| 118 | + assert.EqualValues(t, expectedUpdateFileResponse.Content, filesResponse.Files[1]) |
120 | 119 |
|
121 | | - // test delete file |
122 | | - assert.Nil(t, filesResponse.Files[2]) |
| 120 | + // test commit info |
| 121 | + assert.EqualValues(t, expectedCreateFileResponse.Commit.SHA, filesResponse.Commit.SHA) |
| 122 | + assert.EqualValues(t, expectedCreateFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) |
| 123 | + assert.EqualValues(t, expectedCreateFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email) |
| 124 | + assert.EqualValues(t, expectedCreateFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name) |
| 125 | + assert.EqualValues(t, expectedCreateFileResponse.Commit.Committer.Email, filesResponse.Commit.Committer.Email) |
| 126 | + assert.EqualValues(t, expectedCreateFileResponse.Commit.Committer.Name, filesResponse.Commit.Committer.Name) |
123 | 127 |
|
124 | | - gitRepo.Close() |
| 128 | + // test delete file |
| 129 | + assert.Nil(t, filesResponse.Files[2]) |
| 130 | + |
| 131 | + gitRepo.Close() |
| 132 | + }) |
125 | 133 | } |
126 | 134 |
|
127 | 135 | // Test changing files in a new branch |
|
0 commit comments