Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #198: Avoid getting alerts from Code scanning #201

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions __tests__/unit/git-repo.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import {
commitOptionsForTests,
createTempEmptyDir,
dummyCommitBodyText,
dummyCommitSubjectText,
newSimpleGitWithCommitterIdentity
} from '../../src/__tests__/helpers'

import {CommitBody} from '../../src/commit-body'
import {CommitMessage} from '../../src/commit-message'
import {CommitSubjectParser} from '../../src/commit-subject-parser'
import {GitDirNotInitializedError} from '../../src/errors'
import {GitRepo} from '../../src/git-repo'
import {GitRepoDir} from '../../src/git-repo-dir'

Expand Down Expand Up @@ -37,4 +44,33 @@ describe('GitRepo', () => {

expect(gitRepo.isInitialized()).toBe(true)
})

it('should check if a repo has commits', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:
initialize new git repository, check that there are no comments, add a test comment, check for the comment

const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)
const gitRepo = new GitRepo(gitRepoDir, git)

await gitRepo.init()

const subject = CommitSubjectParser.parseText(dummyCommitSubjectText())
const body = new CommitBody(dummyCommitBodyText())

const commitMessage = new CommitMessage(subject, body)

await expect(gitRepo.hasCommits()).resolves.toBe(false)

await gitRepo.commit(commitMessage, commitOptionsForTests())

await expect(gitRepo.hasCommits()).resolves.toBe(true)
})

it('should fail when the repo has not been initialized and check if it has commits', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ivanramosnet I did not understand the test intention well. Maybe it's only me but I would rephrase it to:

it should fail while checking if the repo has commits when it has not been initialized

const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)
const gitRepo = new GitRepo(gitRepoDir, git)

await expect(gitRepo.hasCommits()).rejects.toThrow(
GitDirNotInitializedError
)
})
})
26 changes: 2 additions & 24 deletions __tests__/unit/queue.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
commitOptionsForTests,
commitOptionsForTestsUsingSignature,
createInitializedGitRepo,
createInitializedTempGnuPGHomeDir,
createNotInitializedGitRepo,
Expand All @@ -8,39 +10,15 @@ import {
gitLogForLatestCommit
} from '../../src/__tests__/helpers'

import {CommitAuthor} from '../../src/commit-author'
import {CommitInfo} from '../../src/commit-info'
import {CommitOptions} from '../../src/commit-options'
import {GitRepo} from '../../src/git-repo'
import {Job} from '../../src/job'
import {Queue} from '../../src/queue'
import {QueueName} from '../../src/queue-name'
import {SigningKeyId} from '../../src/signing-key-id'

import {testConfiguration} from '../../src/__tests__/config'

function commitOptionsForTests(): CommitOptions {
const author = CommitAuthor.fromNameAndEmail(
'A committer',
'committer@example.com'
)
const signingKeyId = new SigningKeyId('')
const noGpgSig = true
return new CommitOptions(author, signingKeyId, noGpgSig)
}

function commitOptionsForTestsUsingSignature(): CommitOptions {
const author = CommitAuthor.fromNameAndEmail(
'A committer',
'committer@example.com'
)
const signingKeyId = new SigningKeyId(
testConfiguration().gpg_signing_key.fingerprint
)
const noGpgSig = false
return new CommitOptions(author, signingKeyId, noGpgSig)
}

async function createTestQueue(
commitOptions: CommitOptions,
queueName = 'queue-name'
Expand Down
6 changes: 0 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import * as openpgp from './openpgp'

import simpleGit, {SimpleGit, SimpleGitOptions} from 'simple-git'

import {CommitAuthor} from '../commit-author'
import {CommitHash} from '../commit-hash'
import {CommitOptions} from '../commit-options'
import {GitRepo} from '../git-repo'
import {GitRepoDir} from '../git-repo-dir'
import {SigningKeyId} from '../signing-key-id'

import {createTempDir} from 'jest-fixtures'
import {join} from 'path'
Expand Down Expand Up @@ -125,3 +128,29 @@ export function dummyCommitBodyText(): string {
}
})
}

export function dummyCommitSubjectText(): string {
return '📝🈺: queue-name: job.ref.f1a69d48a01cc130a64aeac5eaf762e4ba685de7'
}

export function commitOptionsForTests(): CommitOptions {
const author = CommitAuthor.fromNameAndEmail(
'A committer',
'committer@example.com'
)
const signingKeyId = new SigningKeyId('')
const noGpgSig = true
return new CommitOptions(author, signingKeyId, noGpgSig)
}

export function commitOptionsForTestsUsingSignature(): CommitOptions {
const author = CommitAuthor.fromNameAndEmail(
'A committer',
'committer@example.com'
)
const signingKeyId = new SigningKeyId(
testConfiguration().gpg_signing_key.fingerprint
)
const noGpgSig = false
return new CommitOptions(author, signingKeyId, noGpgSig)
}
5 changes: 0 additions & 5 deletions src/git-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ export class GitRepo {
this.git.env(name, value)
}

async getCurrentBranch(): Promise<string | null> {
const status = await this.git.status()
return status.current
}

async log(): Promise<LogResult<DefaultLogFields>> {
return await this.git.log()
}
Expand Down