Skip to content

Commit 426a966

Browse files
committed
e2e: force push test
Signed-off-by: Victoria Dye <[email protected]>
1 parent f72e5a5 commit 426a966

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

test/e2e/features/basic.feature

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ Feature: Basic bundle server usage
3030
Then I am up-to-date with 'main'
3131
Then my repo's bundles are up-to-date with 'main'
3232

33-
# Scenario: Bundle with a force-push
34-
# Given a remote repository with 10 commits to 'main'
35-
# Given the bundle server has been initialized with the local repo
36-
# Given I clone from the local repo with a bundle URI
37-
# Given another developer removes 2 commits and adds 4 commits to 'main'
38-
# Given the bundle server is updated for the local repo
39-
# When I fetch from the remote
40-
# Then I am up-to-date with 'main'
33+
Scenario: A user can fetch force-pushed refs from the bundle server
34+
Given a new remote repository with main branch 'main'
35+
Given another user pushed 10 commits to 'main'
36+
Given the bundle server has been initialized with the remote repo
37+
Given I cloned from the remote repo with a bundle URI
38+
Given another developer removed 2 commits and added 4 commits to 'main'
39+
Given the bundle server was updated for the remote repo
40+
When I fetch from the remote
41+
Then I am up-to-date with 'main'
42+
Then my repo's bundles are up-to-date with 'main'

test/e2e/features/step_definitions/repository.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,7 @@ import { Given, When, Then } from '@cucumber/cucumber'
1111
*/
1212

1313
Given('another user pushed {int} commits to {string}', async function(this: BundleServerWorld, commitNum: number, branch: string) {
14-
if (this.remote && !this.remote.isLocal) {
15-
throw new Error("Remote is not initialized or does not allow pushes")
16-
}
17-
18-
const user = User.Another
19-
if (!this.repoMap.has(user)) {
20-
this.cloneRepositoryFor(user)
21-
utils.assertStatus(0, this.getRepo(user).cloneResult)
22-
}
23-
const clonedRepo = this.getRepo(user)
24-
25-
// TODO: figure out a better way to check whether the repo is empty
26-
if (clonedRepo.runGit("show").status != 0) {
27-
// Repo is empty, so make sure we're on the right branch
28-
clonedRepo.runGit("branch", "-m", branch)
29-
} else {
30-
clonedRepo.runGit("switch", branch)
31-
clonedRepo.runGit("pull", "origin", branch)
32-
}
14+
const clonedRepo = this.getRepoAtBranch(User.Another, branch)
3315

3416
for (let i = 0; i < commitNum; i++) {
3517
utils.assertStatus(0, clonedRepo.runShell(`echo ${randomBytes(16).toString('hex')} >README.md`))
@@ -39,6 +21,25 @@ Given('another user pushed {int} commits to {string}', async function(this: Bund
3921
utils.assertStatus(0, clonedRepo.runGit("push", "origin", branch))
4022
})
4123

24+
Given('another developer removed {int} commits and added {int} commits to {string}',
25+
async function(this: BundleServerWorld, removeCommits: number, addCommits: number, branch: string) {
26+
const clonedRepo = this.getRepoAtBranch(User.Another, branch)
27+
28+
// First, reset
29+
utils.assertStatus(0, clonedRepo.runGit("reset", "--hard", `HEAD~${removeCommits}`))
30+
31+
// Then, add new commits
32+
for (let i = 0; i < addCommits; i++) {
33+
utils.assertStatus(0, clonedRepo.runShell(`echo ${randomBytes(16).toString('hex')} >README.md`))
34+
utils.assertStatus(0, clonedRepo.runGit("add", "README.md"))
35+
utils.assertStatus(0, clonedRepo.runGit("commit", "-m", `test ${i+1}`))
36+
}
37+
38+
// Finally, force push
39+
utils.assertStatus(0, clonedRepo.runGit("push", "-f", "origin", branch))
40+
}
41+
)
42+
4243
Given('I cloned from the remote repo with a bundle URI', async function(this: BundleServerWorld) {
4344
const user = User.Me
4445
this.cloneRepositoryFor(user, this.bundleServer.bundleUri())

test/e2e/features/support/world.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { setWorldConstructor, World, IWorldOptions } from '@cucumber/cucumber'
22
import { randomUUID } from 'crypto'
33
import { RemoteRepo } from '../classes/remote'
4-
import { absPath } from './utils'
4+
import * as utils from './utils'
55
import * as fs from 'fs'
66
import * as path from 'path'
77
import { ClonedRepository } from '../classes/repository'
@@ -35,12 +35,12 @@ export class BundleServerWorld extends World<BundleServerParameters> {
3535
this.repoMap = new Map<User, ClonedRepository>()
3636

3737
// Set up the trash directory
38-
this.trashDirectory = path.join(absPath(this.parameters.trashDirectoryBase), randomUUID())
38+
this.trashDirectory = path.join(utils.absPath(this.parameters.trashDirectoryBase), randomUUID())
3939
fs.mkdirSync(this.trashDirectory, { recursive: true });
4040

4141
// Set up the bundle server
42-
this.bundleServer = new BundleServer(absPath(this.parameters.bundleServerCommand),
43-
absPath(this.parameters.bundleWebServerCommand))
42+
this.bundleServer = new BundleServer(utils.absPath(this.parameters.bundleServerCommand),
43+
utils.absPath(this.parameters.bundleWebServerCommand))
4444
}
4545

4646
cloneRepositoryFor(user: User, bundleUri?: string): void {
@@ -61,6 +61,30 @@ export class BundleServerWorld extends World<BundleServerParameters> {
6161
return repo
6262
}
6363

64+
getRepoAtBranch(user: User, branch: string): ClonedRepository {
65+
if (this.remote && !this.remote.isLocal) {
66+
throw new Error("Remote is not initialized or does not allow pushes")
67+
}
68+
69+
if (!this.repoMap.has(user)) {
70+
this.cloneRepositoryFor(user)
71+
utils.assertStatus(0, this.getRepo(user).cloneResult)
72+
}
73+
74+
const clonedRepo = this.getRepo(user)
75+
76+
// TODO: figure out a better way to check whether the repo is empty
77+
if (clonedRepo.runGit("show").status != 0) {
78+
// Repo is empty, so make sure we're on the right branch
79+
utils.assertStatus(0, clonedRepo.runGit("branch", "-m", branch))
80+
} else {
81+
utils.assertStatus(0, clonedRepo.runGit("switch", branch))
82+
utils.assertStatus(0, clonedRepo.runGit("pull", "origin", branch))
83+
}
84+
85+
return clonedRepo
86+
}
87+
6488
cleanup(): void {
6589
this.bundleServer.cleanup()
6690

0 commit comments

Comments
 (0)