Skip to content

Commit

Permalink
Get rid of temp module which also depends on rimraf
Browse files Browse the repository at this point in the history
  • Loading branch information
niik committed Nov 25, 2024
1 parent 1a28fb4 commit 4cd0740
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 217 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
"devDependencies": {
"@types/node": "20",
"@types/progress": "^2.0.1",
"@types/temp": "^0.9.4",
"node-test-github-reporter": "^1.2.0",
"prettier": "^3.3.1",
"temp": "^0.9.4",
"tsx": "^4.10.5",
"typescript": "^5.4.5"
}
Expand Down
10 changes: 3 additions & 7 deletions test/fast/commit-test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import assert from 'assert'
import { verify } from '../helpers'
import { createTestDir, verify } from '../helpers'

import * as Fs from 'fs'
import * as Path from 'path'

import { track } from 'temp'
import { describe, it } from 'node:test'
import { exec } from '../../lib'

const temp = track()

describe('commit', () => {
it('can commit changes', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-test-commit')
it('can commit changes', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-test-commit')

await exec(['init'], testRepoPath)

Expand Down
8 changes: 3 additions & 5 deletions test/fast/environment-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import assert from 'assert'
import { setupEnvironment } from '../../lib/git-environment'
import { track } from 'temp'
import { describe, it } from 'node:test'
import { exec } from '../../lib'

const temp = track()
import { createTestDir } from '../helpers'

describe('environment variables', () => {
it('can set them', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-test-environment')
it('can set them', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-test-environment')
const result = await exec(['var', 'GIT_AUTHOR_IDENT'], testRepoPath, {
env: {
GIT_AUTHOR_NAME: 'Foo Bar',
Expand Down
24 changes: 12 additions & 12 deletions test/fast/errors-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import assert from 'assert'
import { describe, it } from 'node:test'

describe('detects errors', () => {
it('RemoteAlreadyExists', async () => {
const repoPath = await initialize('remote-already-exists-test-repo')
it('RemoteAlreadyExists', async t => {
const repoPath = await initialize(t, 'remote-already-exists-test-repo')

await exec(['remote', 'add', 'new-remote', 'https://github.com'], repoPath)

Expand All @@ -19,8 +19,8 @@ describe('detects errors', () => {

assertHasGitError(result, GitError.RemoteAlreadyExists)
})
it('TagAlreadyExists', async () => {
const repoPath = await initialize('tag-already-exists-test-repo')
it('TagAlreadyExists', async t => {
const repoPath = await initialize(t, 'tag-already-exists-test-repo')
const filePath = 'text.md'

writeFileSync(join(repoPath, filePath), 'some text')
Expand All @@ -34,17 +34,17 @@ describe('detects errors', () => {

assertHasGitError(result, GitError.TagAlreadyExists)
})
it('BranchAlreadyExists', async () => {
const path = await initialize('branch-already-exists', 'foo')
it('BranchAlreadyExists', async t => {
const path = await initialize(t, 'branch-already-exists', 'foo')
await exec(['commit', '-m', 'initial', '--allow-empty'], path)

const result = await exec(['branch', 'foo'], path)

assertHasGitError(result, GitError.BranchAlreadyExists)
})
it('UnsafeDirectory', async () => {
it('UnsafeDirectory', async t => {
const repoName = 'branch-already-exists'
const path = await initialize(repoName)
const path = await initialize(t, repoName)

const result = await exec(['status'], path, {
env: {
Expand All @@ -65,8 +65,8 @@ describe('detects errors', () => {
assert.ok(m![1].includes(repoName), 'repo name not found in error message')
})
describe('BadConfigValue', () => {
it('detects bad boolean config value', async () => {
const repoPath = await initialize('bad-config-repo')
it('detects bad boolean config value', async t => {
const repoPath = await initialize(t, 'bad-config-repo')

const filePath = 'text.md'
writeFileSync(join(repoPath, filePath), 'some text')
Expand All @@ -83,8 +83,8 @@ describe('detects errors', () => {
assert.equal(errorInfo!.value, 'nab')
assert.equal(errorInfo!.key, 'core.autocrlf')
})
it('detects bad numeric config value', async () => {
const repoPath = await initialize('bad-config-repo')
it('detects bad numeric config value', async t => {
const repoPath = await initialize(t, 'bad-config-repo')

const filePath = 'text.md'
writeFileSync(join(repoPath, filePath), 'some text')
Expand Down
83 changes: 50 additions & 33 deletions test/fast/git-process-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
initializeWithRemote,
gitForWindowsVersion,
assertHasGitError,
createTestDir,
} from '../helpers'

import { gitVersion } from '../helpers'
import { track } from 'temp'
import assert from 'assert'
import { describe, it } from 'node:test'

const temp = track()
import { tmpdir } from 'os'
import { randomBytes } from 'crypto'

describe('git-process', () => {
it('can cancel in-progress git command', async () => {
Expand All @@ -34,8 +34,8 @@ describe('git-process', () => {
assert.equal(result.code, 'ABORT_ERR')
})

it('cannot cancel already finished git command', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-do-nothing')
it('cannot cancel already finished git command', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-do-nothing')
const ac = new AbortController()
const { stdout } = await git(['--version'], testRepoPath, {
signal: ac.signal,
Expand All @@ -60,16 +60,16 @@ describe('git-process', () => {
})

describe('exitCode', () => {
it('returns exit code when folder is empty', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-test-blank')
it('returns exit code when folder is empty', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-test-blank')
const result = await git(['show', 'HEAD'], testRepoPath)
verify(result, r => {
assert.equal(r.exitCode, 128)
})
})

it('handles stdin closed errors', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-test-blank')
it('handles stdin closed errors', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-test-blank')

// Pass an unknown arg to Git, forcing it to terminate immediately
// and then try to write to stdin. Without the ignoreClosedInputStream
Expand All @@ -84,8 +84,8 @@ describe('git-process', () => {
})

describe('diff', () => {
it('returns expected error code for initial commit when creating diff', async () => {
const testRepoPath = await initialize('blank-no-commits')
it('returns expected error code for initial commit when creating diff', async t => {
const testRepoPath = await initialize(t, 'blank-no-commits')

const file = path.join(testRepoPath, 'new-file.md')
fs.writeFileSync(file, 'this is a new file')
Expand All @@ -108,8 +108,8 @@ describe('git-process', () => {
})
})

it('returns expected error code for repository with history when creating diff', async () => {
const testRepoPath = await initialize('blank-then-commit')
it('returns expected error code for repository with history when creating diff', async t => {
const testRepoPath = await initialize(t, 'blank-then-commit')
const readme = path.join(testRepoPath, 'README.md')
fs.writeFileSync(readme, 'hello world!')
await git(['add', '.'], testRepoPath)
Expand Down Expand Up @@ -151,8 +151,8 @@ describe('git-process', () => {
})

describe('show', () => {
it('existing file', async () => {
const testRepoPath = await initialize('desktop-show-existing')
it('existing file', async t => {
const testRepoPath = await initialize(t, 'desktop-show-existing')
const filePath = path.join(testRepoPath, 'file.txt')

fs.writeFileSync(filePath, 'some content', { encoding: 'utf8' })
Expand All @@ -166,15 +166,18 @@ describe('git-process', () => {
assert.equal(r.stdout.trim(), 'some content')
})
})
it('missing from index', async () => {
const testRepoPath = await initialize('desktop-show-missing-index')
it('missing from index', async t => {
const testRepoPath = await initialize(t, 'desktop-show-missing-index')

const result = await git(['show', ':missing.txt'], testRepoPath)

assertHasGitError(result, GitError.PathDoesNotExist)
})
it('missing from commitish', async () => {
const testRepoPath = await initialize('desktop-show-missing-commitish')
it('missing from commitish', async t => {
const testRepoPath = await initialize(
t,
'desktop-show-missing-commitish'
)

const filePath = path.join(testRepoPath, 'file.txt')

Expand All @@ -187,17 +190,18 @@ describe('git-process', () => {

assertHasGitError(result, GitError.PathDoesNotExist)
})
it('invalid object name - empty repository', async () => {
it('invalid object name - empty repository', async t => {
const testRepoPath = await initialize(
t,
'desktop-show-invalid-object-empty'
)

const result = await git(['show', 'HEAD:missing.txt'], testRepoPath)

assertHasGitError(result, GitError.InvalidObjectName)
})
it('outside repository', async () => {
const testRepoPath = await initialize('desktop-show-outside')
it('outside repository', async t => {
const testRepoPath = await initialize(t, 'desktop-show-outside')

const filePath = path.join(testRepoPath, 'file.txt')

Expand Down Expand Up @@ -232,7 +236,10 @@ describe('git-process', () => {
})

it('raises error when folder does not exist', async () => {
const testRepoPath = path.join(temp.path(), 'desktop-does-not-exist')
const testRepoPath = path.join(
tmpdir(),
'desktop-does-not-exist-' + randomBytes(8).toString('hex')
)

let error: Error | null = null
try {
Expand Down Expand Up @@ -500,8 +507,8 @@ mark them as resolved using git add`
assert.equal(error, GitError.HostDown)
})

it('can parse an error when merging with local changes', async () => {
const repoPath = await initialize('desktop-merge-with-local-changes')
it('can parse an error when merging with local changes', async t => {
const repoPath = await initialize(t, 'desktop-merge-with-local-changes')
const readmePath = path.join(repoPath, 'Readme.md')

// Add a commit to the default branch.
Expand Down Expand Up @@ -529,8 +536,8 @@ mark them as resolved using git add`
assertHasGitError(result, GitError.MergeWithLocalChanges)
})

it('can parse an error when renasing with local changes', async () => {
const repoPath = await initialize('desktop-merge-with-local-changes')
it('can parse an error when renasing with local changes', async t => {
const repoPath = await initialize(t, 'desktop-merge-with-local-changes')
const readmePath = path.join(repoPath, 'Readme.md')

// Add a commit to the default branch.
Expand Down Expand Up @@ -558,13 +565,15 @@ mark them as resolved using git add`
assertHasGitError(result, GitError.RebaseWithLocalChanges)
})

it('can parse an error when pulling with merge with local changes', async () => {
it('can parse an error when pulling with merge with local changes', async t => {
const { path: repoPath, remote: remoteRepositoryPath } =
await initializeWithRemote(
t,
'desktop-pullrebase-with-local-changes',
null
)
const { path: forkRepoPath } = await initializeWithRemote(
t,
'desktop-pullrebase-with-local-changes-fork',
remoteRepositoryPath
)
Expand Down Expand Up @@ -600,13 +609,15 @@ mark them as resolved using git add`
assertHasGitError(result, GitError.MergeWithLocalChanges)
})

it('can parse an error when pulling with rebase with local changes', async () => {
it('can parse an error when pulling with rebase with local changes', async t => {
const { path: repoPath, remote: remoteRepositoryPath } =
await initializeWithRemote(
t,
'desktop-pullrebase-with-local-changes',
null
)
const { path: forkRepoPath } = await initializeWithRemote(
t,
'desktop-pullrebase-with-local-changes-fork',
remoteRepositoryPath
)
Expand Down Expand Up @@ -642,8 +653,11 @@ mark them as resolved using git add`
assertHasGitError(result, GitError.RebaseWithLocalChanges)
})

it('can parse an error when there is a conflict while merging', async () => {
const repoPath = await initialize('desktop-pullrebase-with-local-changes')
it('can parse an error when there is a conflict while merging', async t => {
const repoPath = await initialize(
t,
'desktop-pullrebase-with-local-changes'
)
const readmePath = path.join(repoPath, 'Readme.md')

// Create a commit on the default branch.
Expand Down Expand Up @@ -676,8 +690,11 @@ mark them as resolved using git add`
assertHasGitError(result, GitError.MergeConflicts)
})

it('can parse an error when there is a conflict while rebasing', async () => {
const repoPath = await initialize('desktop-pullrebase-with-local-changes')
it('can parse an error when there is a conflict while rebasing', async t => {
const repoPath = await initialize(
t,
'desktop-pullrebase-with-local-changes'
)
const readmePath = path.join(repoPath, 'Readme.md')

// Create a commit on the default branch.
Expand Down
13 changes: 5 additions & 8 deletions test/fast/lfs-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import assert from 'assert'
import { gitLfsVersion } from '../helpers'
import { track } from 'temp'
import { createTestDir, gitLfsVersion } from '../helpers'
import { describe, it } from 'node:test'
import { exec } from '../../lib'

const temp = track()

describe('lfs', () => {
it('can be resolved', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-lfs')
it('can be resolved', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-lfs')
const result = await exec(['lfs'], testRepoPath)
assert.equal(result.exitCode, 0)
assert.ok(
Expand All @@ -19,8 +16,8 @@ describe('lfs', () => {
)
})

it('matches the expected version', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-lfs')
it('matches the expected version', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-lfs')
const result = await exec(['lfs', 'version'], testRepoPath)
assert.equal(result.exitCode, 0)
assert.ok(
Expand Down
10 changes: 3 additions & 7 deletions test/fast/status-test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { verify } from '../helpers'
import { track } from 'temp'
import { createTestDir, verify } from '../helpers'

import * as Fs from 'fs'
import * as Path from 'path'
import assert from 'assert'
import { describe, it } from 'node:test'
import { exec } from '../../lib'

const temp = track()

describe('status', () => {
it('lists untracked file', async () => {
const testRepoPath = temp.mkdirSync('desktop-git-test-commit')

it('lists untracked file', async t => {
const testRepoPath = await createTestDir(t, 'desktop-git-test-commit')
await exec(['init'], testRepoPath)

const readme = Path.join(testRepoPath, 'README.md')
Expand Down
Loading

0 comments on commit 4cd0740

Please sign in to comment.