Skip to content

Commit

Permalink
fix: Use resolved git root on Windows (#26179)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-plummer authored Mar 27, 2023
1 parent 267b666 commit 91cc2e2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 49 deletions.
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _Released 03/28/2023 (PENDING)_
- Fixed a compatibility issue so that component test projects can use [Vite](https://vitejs.dev/) version 4.2.0 and greater. Fixes [#26138](https://github.com/cypress-io/cypress/issues/26138).
- Changed the way that Git hashes are loaded so that non-relevant runs are excluded from the Debug page. Fixes [#26058](https://github.com/cypress-io/cypress/issues/26058).
- Fixed an issue where [`cy.intercept()`](https://docs.cypress.io/api/commands/intercept) added an additional `content-length` header to spied requests that did not set a `content-length` header on the original request. Fixes [#24407](https://github.com/cypress-io/cypress/issues/24407).
- Fixed an issue where an incorrect working directory could be used for Git operations on Windows. Fixes [#23317](https://github.com/cypress-io/cypress/issues/23317).

**Misc:**

Expand Down
14 changes: 12 additions & 2 deletions packages/data-context/src/sources/GitDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export class GitDataSource {
debug('exception caught when loading git client')
}

// Start by assuming the git repository matches the project root
// This will be overridden if needed by the `verifyGitRepo` function
// Since that is async and we can't block the constructor we make this
// guess to avoid double-initializing
this.#gitBaseDir = this.config.projectRoot

// don't watch/refresh git data in run mode since we only
// need it to detect the .git directory to set `repoRoot`
if (config.isRunMode) {
Expand Down Expand Up @@ -243,6 +249,8 @@ export class GitDataSource {
debug(`Failed to watch for git changes`, e.message)
this.config.onError(e)
})

debug('Watcher initialized')
}
} catch (e) {
this.#gitErrored = true
Expand Down Expand Up @@ -403,9 +411,9 @@ export class GitDataSource {
const cmd = `FOR %x in (${paths}) DO (${GIT_LOG_COMMAND} %x)`

debug('executing command: `%s`', cmd)
debug('cwd: `%s`', this.config.projectRoot)
debug('cwd: `%s`', this.#gitBaseDir)

const subprocess = execa(cmd, { shell: true, cwd: this.config.projectRoot })
const subprocess = execa(cmd, { shell: true, cwd: this.#gitBaseDir })
let result

try {
Expand Down Expand Up @@ -437,6 +445,8 @@ export class GitDataSource {
debug('Loading git hashes')
try {
const logResponse = await this.#git?.log({ maxCount: 100, '--first-parent': undefined })

debug('hashes loaded')
const currentHashes = logResponse?.all.map((log) => log.hash)

if (!isEqual(this.#gitHashes, currentHashes)) {
Expand Down
79 changes: 33 additions & 46 deletions packages/data-context/test/unit/sources/GitDataSource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { assert, expect } from 'chai'
import path from 'path'
import os from 'os'
import simpleGit from 'simple-git'
Expand All @@ -8,7 +8,7 @@ import pDefer from 'p-defer'
import chokidar from 'chokidar'

import { scaffoldMigrationProject } from '../helper'
import { GitDataSource, GitInfo } from '../../../src/sources/GitDataSource'
import { GitDataSource } from '../../../src/sources/GitDataSource'
import { toPosix } from '../../../src/util/file'

describe('GitDataSource', () => {
Expand All @@ -21,7 +21,7 @@ describe('GitDataSource', () => {
projectPath = await scaffoldMigrationProject('e2e')
git = simpleGit({ baseDir: projectPath })
e2eFolder = path.join(projectPath, 'cypress', 'e2e')
const allSpecs = fs.readdirSync(e2eFolder)
const allSpecs = await fs.readdir(e2eFolder)

if (process.env.CI) {
// need to set a user on CI
Expand All @@ -36,21 +36,19 @@ describe('GitDataSource', () => {
await git.commit('add all specs')
})

afterEach(() => {
afterEach(async () => {
if (gitInfo) {
gitInfo.destroy()
await gitInfo.destroy()
}

gitInfo = undefined

sinon.restore()
})

// TODO: fix flaky test https://github.com/cypress-io/cypress/issues/23317
it.skip(`gets correct status for files on ${os.platform()}`, async function () {
it(`gets correct status for files on ${os.platform()}`, async function () {
const onBranchChange = sinon.stub()
const onGitInfoChange = sinon.stub()
const onError = sinon.stub()
const dfd = pDefer()

// create a file and modify a file to express all
// git states we are interested in (created, unmodified, modified)
Expand All @@ -62,28 +60,24 @@ describe('GitDataSource', () => {
isRunMode: false,
projectRoot: projectPath,
onBranchChange,
onGitInfoChange,
onError,
onGitInfoChange: dfd.resolve,
onError: (err: any) => {
assert.fail(err)
},
})

fs.createFileSync(fooSpec)
fs.writeFileSync(xhrSpec, 'it(\'modifies the file\', () => {})')
await fs.createFile(fooSpec)
await fs.writeFile(xhrSpec, 'it(\'modifies the file\', () => {})')

gitInfo.setSpecs([fooSpec, aRecordSpec, xhrSpec])

let result: any[] = []

do {
result = await Promise.all([
gitInfo.gitInfoFor(fooSpec),
gitInfo.gitInfoFor(aRecordSpec),
gitInfo.gitInfoFor(xhrSpec),
])
const gitInfoChangeResolve = await dfd.promise

await new Promise((resolve) => setTimeout(resolve, 100))
} while (result.some((r) => r == null))
expect(gitInfoChangeResolve).to.eql([fooSpec, aRecordSpec, xhrSpec])

const [created, unmodified, modified] = result
const created = gitInfo.gitInfoFor(fooSpec)!
const unmodified = gitInfo.gitInfoFor(aRecordSpec)!
const modified = gitInfo.gitInfoFor(xhrSpec)!

expect(created.lastModifiedHumanReadable).to.match(/(a few|[0-9]) seconds? ago/)
expect(created.statusType).to.eql('created')
Expand Down Expand Up @@ -129,29 +123,27 @@ describe('GitDataSource', () => {
.map((filename) => path.join(e2eFolder, filename))
.map((filepath) => toPosix(filepath))

const dfd = pDefer()

gitInfo = new GitDataSource({
isRunMode: false,
projectRoot: projectPath,
onBranchChange: sinon.stub(),
onGitInfoChange: sinon.stub(),
onGitInfoChange: dfd.resolve,
onError: sinon.stub(),
})

for (let filepath of filepaths) {
fs.createFileSync(filepath)
}
await Promise.all(
filepaths.map((filepath) => fs.createFile(filepath)),
)

gitInfo.setSpecs(filepaths)

let results: (GitInfo | null)[] = []

do {
results = await Promise.all(filepaths.map(function (filepath) {
return gitInfo.gitInfoFor(filepath)
}))
await dfd.promise

await new Promise((resolve) => setTimeout(resolve, 100))
} while (results.some((r) => r == null))
const results = filepaths.map((filepath) => {
return gitInfo.gitInfoFor(filepath)
})

expect(results).to.have.lengthOf(filepaths.length)

Expand Down Expand Up @@ -217,7 +209,6 @@ describe('GitDataSource', () => {
onError: errorStub,
})

await dfd.promise
const result = await dfd.promise

expect(result).to.eq((await git.branch()).current)
Expand All @@ -226,7 +217,7 @@ describe('GitDataSource', () => {
})

context('Git hashes', () => {
let clock
let clock: sinon.SinonFakeTimers

beforeEach(() => {
clock = sinon.useFakeTimers()
Expand All @@ -239,17 +230,13 @@ describe('GitDataSource', () => {
it('loads git hashes when first loaded', async () => {
const dfd = pDefer()

const logCallback = () => {
dfd.resolve()
}

gitInfo = new GitDataSource({
isRunMode: false,
projectRoot: projectPath,
onBranchChange: sinon.stub(),
onGitInfoChange: sinon.stub(),
onError: sinon.stub(),
onGitLogChange: logCallback,
onGitLogChange: dfd.resolve,
})

await dfd.promise
Expand Down Expand Up @@ -281,7 +268,7 @@ describe('GitDataSource', () => {

const afterCommitSpec = toPosix(path.join(e2eFolder, 'afterCommit.cy.js'))

fs.createFileSync(afterCommitSpec)
await fs.createFile(afterCommitSpec)

git.add(afterCommitSpec)
git.commit('add afterCommit spec')
Expand Down Expand Up @@ -310,7 +297,7 @@ describe('GitDataSource', () => {

const newSpec = toPosix(path.join(e2eFolder, 'new.cy.js'))

fs.createFileSync(newSpec)
await fs.createFile(newSpec)

await git.add([newSpec])

Expand All @@ -322,7 +309,7 @@ describe('GitDataSource', () => {

const featureSpec = toPosix(path.join(e2eFolder, 'feature.cy.js'))

fs.createFileSync(featureSpec)
await fs.createFile(featureSpec)

await git.add([featureSpec])
await git.commit('add feature spec')
Expand Down
1 change: 0 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"schedule": [
"before 5am every weekday"
],

"packageRules": [
{
"matchPackagePatterns": [
Expand Down

5 comments on commit 91cc2e2

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91cc2e2 Mar 27, 2023

Choose a reason for hiding this comment

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

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.8.2/linux-arm64/develop-91cc2e22f2765d40f657016dd01121656449bf36/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91cc2e2 Mar 27, 2023

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.8.2/linux-x64/develop-91cc2e22f2765d40f657016dd01121656449bf36/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91cc2e2 Mar 27, 2023

Choose a reason for hiding this comment

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

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.8.2/darwin-arm64/develop-91cc2e22f2765d40f657016dd01121656449bf36/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91cc2e2 Mar 27, 2023

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.8.2/darwin-x64/develop-91cc2e22f2765d40f657016dd01121656449bf36/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91cc2e2 Mar 27, 2023

Choose a reason for hiding this comment

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

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.8.2/win32-x64/develop-91cc2e22f2765d40f657016dd01121656449bf36/cypress.tgz

Please sign in to comment.