Skip to content

Commit d056872

Browse files
committed
test: add shared directory
There are certain classes currently in the e2e 'support' and 'classes' directories that will be needed by integration tests. This change moves these functions into a new 'shared' directory. Note that the absPath function for utils was left in the original 'utils' file given it finds paths relative to test/e2e. Utils imports have been prefixed with 'local'/'shared' to account for the two files. Signed-off-by: Lessley Dennington <[email protected]>
1 parent 78752c1 commit d056872

File tree

9 files changed

+55
-54
lines changed

9 files changed

+55
-54
lines changed

test/e2e/features/classes/bundleServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { randomBytes } from 'crypto'
22
import * as child_process from 'child_process'
3-
import { RemoteRepo } from './remote'
3+
import { RemoteRepo } from '../../../shared/classes/remote'
44

55
export class BundleServer {
66
private bundleServerCmd: string

test/e2e/features/step_definitions/bundleServer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as utils from '../support/utils'
1+
import * as shared_utils from '../../../shared/support/utils'
22
import { BundleServerWorld, } from '../support/world'
33
import { Given } from '@cucumber/cucumber'
44

@@ -10,9 +10,9 @@ Given('the bundle server has been initialized with the remote repo', async funct
1010
if (this.remote === undefined) {
1111
throw new Error("Remote repository is not initialized")
1212
}
13-
utils.assertStatus(0, this.bundleServer.init(this.remote))
13+
shared_utils.assertStatus(0, this.bundleServer.init(this.remote))
1414
})
1515

1616
Given('the bundle server was updated for the remote repo', async function (this: BundleServerWorld) {
17-
utils.assertStatus(0, this.bundleServer.update())
17+
shared_utils.assertStatus(0, this.bundleServer.update())
1818
})

test/e2e/features/step_definitions/remote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Given, } from '@cucumber/cucumber'
2-
import { RemoteRepo } from '../classes/remote'
2+
import { RemoteRepo } from '../../../shared/classes/remote'
33
import { BundleServerWorld } from '../support/world'
44
import * as path from 'path'
55

test/e2e/features/step_definitions/repository.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from 'assert'
2-
import * as utils from '../support/utils'
2+
import * as shared_utils from '../../../shared/support/utils'
33
import { randomBytes } from 'crypto'
44
import { BundleServerWorld, User } from '../support/world'
55
import { Given, When, Then } from '@cucumber/cucumber'
@@ -14,36 +14,36 @@ Given('another user pushed {int} commits to {string}', async function (this: Bun
1414
const clonedRepo = this.getRepoAtBranch(User.Another, branch)
1515

1616
for (let i = 0; i < commitNum; i++) {
17-
utils.assertStatus(0, clonedRepo.runShell(`echo ${randomBytes(16).toString('hex')} >README.md`))
18-
utils.assertStatus(0, clonedRepo.runGit("add", "README.md"))
19-
utils.assertStatus(0, clonedRepo.runGit("commit", "-m", `test ${i + 1}`))
17+
shared_utils.assertStatus(0, clonedRepo.runShell(`echo ${randomBytes(16).toString('hex')} >README.md`))
18+
shared_utils.assertStatus(0, clonedRepo.runGit("add", "README.md"))
19+
shared_utils.assertStatus(0, clonedRepo.runGit("commit", "-m", `test ${i + 1}`))
2020
}
21-
utils.assertStatus(0, clonedRepo.runGit("push", "origin", branch))
21+
shared_utils.assertStatus(0, clonedRepo.runGit("push", "origin", branch))
2222
})
2323

2424
Given('another user removed {int} commits and added {int} commits to {string}',
2525
async function (this: BundleServerWorld, removeCommits: number, addCommits: number, branch: string) {
2626
const clonedRepo = this.getRepoAtBranch(User.Another, branch)
2727

2828
// First, reset
29-
utils.assertStatus(0, clonedRepo.runGit("reset", "--hard", `HEAD~${removeCommits}`))
29+
shared_utils.assertStatus(0, clonedRepo.runGit("reset", "--hard", `HEAD~${removeCommits}`))
3030

3131
// Then, add new commits
3232
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}`))
33+
shared_utils.assertStatus(0, clonedRepo.runShell(`echo ${randomBytes(16).toString('hex')} >README.md`))
34+
shared_utils.assertStatus(0, clonedRepo.runGit("add", "README.md"))
35+
shared_utils.assertStatus(0, clonedRepo.runGit("commit", "-m", `test ${i + 1}`))
3636
}
3737

3838
// Finally, force push
39-
utils.assertStatus(0, clonedRepo.runGit("push", "-f", "origin", branch))
39+
shared_utils.assertStatus(0, clonedRepo.runGit("push", "-f", "origin", branch))
4040
}
4141
)
4242

4343
Given('I cloned from the remote repo with a bundle URI', async function (this: BundleServerWorld) {
4444
const user = User.Me
4545
this.cloneRepositoryFor(user, this.bundleServer.bundleUri())
46-
utils.assertStatus(0, this.getRepo(user).cloneResult)
46+
shared_utils.assertStatus(0, this.getRepo(user).cloneResult)
4747
})
4848

4949
When('I clone from the remote repo with a bundle URI', async function (this: BundleServerWorld) {
@@ -56,14 +56,14 @@ When('another developer clones from the remote repo without a bundle URI', async
5656

5757
When('I fetch from the remote', async function (this: BundleServerWorld) {
5858
const clonedRepo = this.getRepo(User.Me)
59-
utils.assertStatus(0, clonedRepo.runGit("fetch", "origin"))
59+
shared_utils.assertStatus(0, clonedRepo.runGit("fetch", "origin"))
6060
})
6161

6262
Then('bundles are downloaded and used', async function (this: BundleServerWorld) {
6363
const clonedRepo = this.getRepo(User.Me)
6464

6565
// Verify the clone executed as-expected
66-
utils.assertStatus(0, clonedRepo.cloneResult, "git clone failed")
66+
shared_utils.assertStatus(0, clonedRepo.cloneResult, "git clone failed")
6767

6868
// Ensure warning wasn't thrown
6969
clonedRepo.cloneResult.stderr.toString().split("\n").forEach(function (line) {
@@ -74,12 +74,12 @@ Then('bundles are downloaded and used', async function (this: BundleServerWorld)
7474

7575
// Make sure the config is set up properly
7676
let result = clonedRepo.runGit("config", "--get", "fetch.bundleURI")
77-
utils.assertStatus(0, result, "'fetch.bundleURI' is not set after clone")
77+
shared_utils.assertStatus(0, result, "'fetch.bundleURI' is not set after clone")
7878
const actualURI = result.stdout.toString().trim()
7979
assert.strictEqual(actualURI, this.bundleServer.bundleUri())
8080

8181
result = clonedRepo.runGit("for-each-ref", "--format=%(refname)", "refs/bundles/*")
82-
utils.assertStatus(0, result, "git for-each-ref failed")
82+
shared_utils.assertStatus(0, result, "git for-each-ref failed")
8383

8484
const bundleRefs = result.stdout.toString().split("\n").filter(function(line) {
8585
return line.trim() != ""
@@ -90,7 +90,7 @@ Then('bundles are downloaded and used', async function (this: BundleServerWorld)
9090
Then('I am up-to-date with {string}', async function (this: BundleServerWorld, branch: string) {
9191
const clonedRepo = this.getRepo(User.Me)
9292
const result = clonedRepo.runGit("rev-parse", `refs/remotes/origin/${branch}`)
93-
utils.assertStatus(0, result)
93+
shared_utils.assertStatus(0, result)
9494
const actualOid = result.stdout.toString().trim()
9595
const expectedOid = this.remote?.getBranchTipOid(branch)
9696
assert.strictEqual(actualOid, expectedOid, `branch '${branch}' is not up-to-date`)
@@ -100,7 +100,7 @@ Then('my repo\'s bundles {boolean} up-to-date with {string}',
100100
async function (this: BundleServerWorld, expectedUpToDate: boolean, branch: string) {
101101
const clonedRepo = this.getRepo(User.Me)
102102
const result = clonedRepo.runGit("rev-parse", `refs/bundles/${branch}`)
103-
utils.assertStatus(0, result)
103+
shared_utils.assertStatus(0, result)
104104
const actualOid = result.stdout.toString().trim()
105105
const expectedOid = this.remote?.getBranchTipOid(branch)
106106

@@ -117,8 +117,8 @@ Then('I compare the clone execution times', async function (this: BundleServerWo
117117
const otherClone = this.getRepo(User.Another)
118118

119119
// Verify the clones succeeded
120-
utils.assertStatus(0, myClone.cloneResult)
121-
utils.assertStatus(0, otherClone.cloneResult)
120+
shared_utils.assertStatus(0, myClone.cloneResult)
121+
shared_utils.assertStatus(0, otherClone.cloneResult)
122122

123123
console.log(`\nClone execution time for ${this.remote!.remoteUri}: ${(myClone.cloneTimeMs / 1000).toFixed(2)}s (bundle URI) vs. ${(otherClone.cloneTimeMs / 1000).toFixed(2)}s (no bundle URI)`)
124124
})

test/e2e/features/support/utils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import * as path from 'path'
2-
import * as assert from 'assert'
3-
import * as child_process from 'child_process'
4-
5-
export function runGit(...args: string[]): child_process.SpawnSyncReturns<Buffer> {
6-
return child_process.spawnSync("git", args)
7-
}
82

93
export function absPath(pathParam: string): string {
104
// Convert a given path (either relative to 'test/e2e/' or absolute) to an
@@ -15,11 +9,3 @@ export function absPath(pathParam: string): string {
159
return pathParam
1610
}
1711
}
18-
19-
export function assertStatus(expectedStatusCode: number, result: child_process.SpawnSyncReturns<Buffer>, message?: string): void {
20-
if (result.error) {
21-
throw result.error
22-
}
23-
assert.strictEqual(result.status, expectedStatusCode,
24-
`${message ?? "Invalid status code"}:\n\tstdout: ${result.stdout.toString()}\n\tstderr: ${result.stderr.toString()}`)
25-
}

test/e2e/features/support/world.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { setWorldConstructor, World, IWorldOptions } from '@cucumber/cucumber'
22
import { randomUUID } from 'crypto'
3-
import { RemoteRepo } from '../classes/remote'
4-
import * as utils from './utils'
3+
import { RemoteRepo } from '../../../shared/classes/remote'
4+
import * as local_utils from './utils'
5+
import * as shared_utils from '../../../shared/support/utils'
56
import * as fs from 'fs'
67
import * as path from 'path'
7-
import { ClonedRepository } from '../classes/repository'
8+
import { ClonedRepository } from '../../../shared/classes/repository'
89
import { BundleServer } from '../classes/bundleServer'
910

1011
export enum User {
@@ -35,12 +36,12 @@ export class BundleServerWorld extends World<BundleServerParameters> {
3536
this.repoMap = new Map<User, ClonedRepository>()
3637

3738
// Set up the trash directory
38-
this.trashDirectory = path.join(utils.absPath(this.parameters.trashDirectoryBase), randomUUID())
39+
this.trashDirectory = path.join(local_utils.absPath(this.parameters.trashDirectoryBase), randomUUID())
3940
fs.mkdirSync(this.trashDirectory, { recursive: true });
4041

4142
// Set up the bundle server
42-
this.bundleServer = new BundleServer(utils.absPath(this.parameters.bundleServerCommand),
43-
utils.absPath(this.parameters.bundleWebServerCommand))
43+
this.bundleServer = new BundleServer(local_utils.absPath(this.parameters.bundleServerCommand),
44+
local_utils.absPath(this.parameters.bundleWebServerCommand))
4445
}
4546

4647
cloneRepositoryFor(user: User, bundleUri?: string): void {
@@ -68,18 +69,18 @@ export class BundleServerWorld extends World<BundleServerParameters> {
6869

6970
if (!this.repoMap.has(user)) {
7071
this.cloneRepositoryFor(user)
71-
utils.assertStatus(0, this.getRepo(user).cloneResult)
72+
shared_utils.assertStatus(0, this.getRepo(user).cloneResult)
7273
}
7374

7475
const clonedRepo = this.getRepo(user)
7576

7677
const result = clonedRepo.runGit("rev-list", "--all", "-n", "1")
7778
if (result.stdout.toString().trim() == "") {
7879
// Repo is empty, so make sure we're on the right branch
79-
utils.assertStatus(0, clonedRepo.runGit("branch", "-m", branch))
80+
shared_utils.assertStatus(0, clonedRepo.runGit("branch", "-m", branch))
8081
} else {
81-
utils.assertStatus(0, clonedRepo.runGit("switch", branch))
82-
utils.assertStatus(0, clonedRepo.runGit("pull", "origin", branch))
82+
shared_utils.assertStatus(0, clonedRepo.runGit("switch", branch))
83+
shared_utils.assertStatus(0, clonedRepo.runGit("pull", "origin", branch))
8384
}
8485

8586
return clonedRepo

test/e2e/features/classes/remote.ts renamed to test/shared/classes/remote.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'path'
2-
import * as utils from '../support/utils'
2+
import * as shared_utils from '../support/utils'
33
import * as child_process from 'child_process'
44

55
export class RemoteRepo {
@@ -25,9 +25,9 @@ export class RemoteRepo {
2525
throw new Error("'urlOrPath' must be a 'file://' URL or absolute path")
2626
}
2727

28-
utils.assertStatus(0, utils.runGit("init", "--bare", this.root))
28+
shared_utils.assertStatus(0, shared_utils.runGit("init", "--bare", this.root))
2929
if (mainBranch) {
30-
utils.assertStatus(0, utils.runGit("-C", this.root, "symbolic-ref", "HEAD", `refs/heads/${mainBranch}`))
30+
shared_utils.assertStatus(0, shared_utils.runGit("-C", this.root, "symbolic-ref", "HEAD", `refs/heads/${mainBranch}`))
3131
}
3232
}
3333
}
@@ -37,7 +37,7 @@ export class RemoteRepo {
3737
throw new Error("Logged branch tips are only available for local custom remotes")
3838
}
3939
const result = child_process.spawnSync("cat", [`refs/heads/${branch}`], { shell: true, cwd: this.root })
40-
utils.assertStatus(0, result)
40+
shared_utils.assertStatus(0, result)
4141
return result.stdout.toString().trim()
4242
}
4343
}

test/e2e/features/classes/repository.ts renamed to test/shared/classes/repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as child_process from 'child_process'
22
import { RemoteRepo } from './remote'
3-
import * as utils from '../support/utils'
3+
import * as shared_utils from '../support/utils'
44

55
export class ClonedRepository {
66
private initialized: boolean
@@ -41,6 +41,6 @@ export class ClonedRepository {
4141
if (!this.initialized) {
4242
throw new Error("Repository is not initialized")
4343
}
44-
return utils.runGit("-C", this.root, ...args)
44+
return shared_utils.runGit("-C", this.root, ...args)
4545
}
4646
}

test/shared/support/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as assert from 'assert'
2+
import * as child_process from 'child_process'
3+
4+
export function runGit(...args: string[]): child_process.SpawnSyncReturns<Buffer> {
5+
return child_process.spawnSync("git", args)
6+
}
7+
8+
export function assertStatus(expectedStatusCode: number, result: child_process.SpawnSyncReturns<Buffer>, message?: string): void {
9+
if (result.error) {
10+
throw result.error
11+
}
12+
assert.strictEqual(result.status, expectedStatusCode,
13+
`${message ?? "Invalid status code"}:\n\tstdout: ${result.stdout.toString()}\n\tstderr: ${result.stderr.toString()}`)
14+
}

0 commit comments

Comments
 (0)