Skip to content

Commit

Permalink
feat: [#38] Replace executeAction calls with helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yeraydavidrodriguez committed May 13, 2022
1 parent 37f373f commit 68cf24a
Showing 1 changed file with 50 additions and 88 deletions.
138 changes: 50 additions & 88 deletions __tests__/e2e/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,47 @@ function executeAction(env): string | Buffer {
return output
}

function createJob(gitRepo: GitRepo): string | Buffer {
const env = {
function createSampleJob(gitRepo: GitRepo, payload: String): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'create-job',
INPUT_JOB_PAYLOAD: dummyPayload(),
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
}
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: payload
})
}

function nextSampleJob(gitRepo: GitRepo): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'next-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
})
}

return executeAction(env)
function startSampleJob(gitRepo: GitRepo, payload: String): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: payload
})
}

function finishSampleJob(gitRepo: GitRepo, payload: String): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'finish-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: payload
})
}

/**
Expand All @@ -72,16 +102,7 @@ describe('GitHub Action', () => {
it('should print the git repo dir at the beginning of the action execution', async () => {
const gitRepo = await createInitializedGitRepo()

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'next-job',
INPUT_JOB_PAYLOAD: dummyPayload(),
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
}

const output = executeAction(env)
const output = nextSampleJob(gitRepo)

expect(output).toEqual(
expect.stringContaining(`git_repo_dir: ${gitRepo.getDirPath()}`)
Expand Down Expand Up @@ -112,16 +133,7 @@ describe('GitHub Action', () => {
it('should create a new job', async () => {
const gitRepo = await createInitializedGitRepo()

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'create-job',
INPUT_JOB_PAYLOAD: dummyPayload(),
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
}

const output = executeAction(env)
const output = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_created', output.toString())).toBe('true')
expect(getOutputVariable('job_commit', output.toString())).toBe(
Expand All @@ -132,17 +144,8 @@ describe('GitHub Action', () => {
it('should get the next job', async () => {
const gitRepo = await createInitializedGitRepo()

createJob(gitRepo)

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'next-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
}

const output = executeAction(env)
createSampleJob(gitRepo, dummyPayload())
const output = nextSampleJob(gitRepo)

expect(getOutputVariable('job_payload', output.toString())).toBe(
dummyPayload()
Expand All @@ -155,18 +158,9 @@ describe('GitHub Action', () => {
it('should mark the pending job as started', async () => {
const gitRepo = await createInitializedGitRepo()

createJob(gitRepo)

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
}
createSampleJob(gitRepo, dummyPayload())

const output = executeAction(env)
const output = startSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_started', output.toString())).toBe('true')
expect(getOutputVariable('job_commit', output.toString())).toBe(
Expand All @@ -177,25 +171,9 @@ describe('GitHub Action', () => {
it('should mark the pending job as finished', async () => {
const gitRepo = await createInitializedGitRepo()

createJob(gitRepo)

executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
})

const output = executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'finish-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
})
createSampleJob(gitRepo, dummyPayload())
startSampleJob(gitRepo, 'test')
const output = finishSampleJob(gitRepo, 'test')

expect(getOutputVariable('job_finished', output.toString())).toBe('true')
expect(getOutputVariable('job_commit', output.toString())).toBe(
Expand All @@ -206,37 +184,21 @@ describe('GitHub Action', () => {
it('should assign job id 0 to the first created job', async () => {
const gitRepo = await createInitializedGitRepo()

const output = createJob(gitRepo)
const output = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_id', output.toString())).toBe('0')
})

it('should assign job id 1 to the second created job', async () => {
const gitRepo = await createInitializedGitRepo()

const firstOutput = createJob(gitRepo)
const firstOutput = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_id', firstOutput.toString())).toBe('0')
startSampleJob(gitRepo, dummyPayload())
finishSampleJob(gitRepo, dummyPayload())

executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
})

executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'finish-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
})

const output = createJob(gitRepo)
const output = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_id', output.toString())).toBe('1')
})
Expand Down

0 comments on commit 68cf24a

Please sign in to comment.