Skip to content

dependency: update trash dep and convert trash files from JS to TS #31667

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

Merged
merged 15 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 4 additions & 12 deletions .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mainBuildFilters: &mainBuildFilters
- /^release\/\d+\.\d+\.\d+$/
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- 'update-v8-snapshot-cache-on-develop'
- 'ryanm/fix/having-trouble-debugging-your-ci-failures'
- 'update-trash'

# usually we don't build Mac app - it takes a long time
# but sometimes we want to really confirm we are doing the right thing
Expand All @@ -49,11 +49,7 @@ macWorkflowFilters: &darwin-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal:
[
'ryanm/fix/having-trouble-debugging-your-ci-failures',
<< pipeline.git.branch >>
]
- equal: [ 'update-trash', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand All @@ -64,11 +60,7 @@ linuxArm64WorkflowFilters: &linux-arm64-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal:
[
'ryanm/fix/having-trouble-debugging-your-ci-failures',
<< pipeline.git.branch >>
]
- equal: [ 'update-trash', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand Down Expand Up @@ -165,7 +157,7 @@ commands:
name: Set environment variable to determine whether or not to persist artifacts
command: |
echo "Setting SHOULD_PERSIST_ARTIFACTS variable"
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "ryanm/fix/having-trouble-debugging-your-ci-failures" ]]; then
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "update-trash" ]]; then
export SHOULD_PERSIST_ARTIFACTS=true
fi' >> "$BASH_ENV"
# You must run `setup_should_persist_artifacts` command and be using bash before running this command
Expand Down
9 changes: 9 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 14.3.4

_Released 5/20/2025 (PENDING)_

**Dependency Updates:**

- Upgraded `trash` from `5.2.0` to `7.2.0`. Addressed in [#31667](https://github.com/cypress-io/cypress/pull/31667).


## 14.3.3

_Released 5/6/2025_
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/modes/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async function trashAssets (config: Cfg) {
try {
await Promise.all([
trash.folder(config.videosFolder),
trash.folder(config.screenshotsFolder),
typeof config.screenshotsFolder === 'string' ? trash.folder(config.screenshotsFolder) : Promise.resolve(),
trash.folder(config.downloadsFolder),
])
} catch (err) {
Expand Down
20 changes: 0 additions & 20 deletions packages/server/lib/util/trash.js

This file was deleted.

34 changes: 34 additions & 0 deletions packages/server/lib/util/trash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { fs } from './fs'
Copy link
Member Author

Choose a reason for hiding this comment

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

This was rewritten a bit, so review carefully

import os from 'os'
import path from 'path'
import trash from 'trash'
import Bluebird from 'bluebird'

// Moves a folder's contents to the trash (or empties it on Linux)
export const folder = async (pathToFolder: string): Promise<void> => {
try {
await fs.statAsync(pathToFolder)

if (os.platform() === 'linux') {
await fs.emptyDir(pathToFolder)

return
}

const items = await fs.readdir(pathToFolder)

await Bluebird.map(items, (item: string) => {
return trash([path.join(pathToFolder, item)])
})
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return
}

throw error
}
}

export default {
folder,
}
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"term-size": "2.1.0",
"through": "2.3.8",
"tough-cookie": "4.1.3",
"trash": "5.2.0",
"trash": "7.2.0",
"ts-node": "^10.9.2",
"tslib": "2.3.1",
"underscore.string": "3.3.6",
Expand Down
77 changes: 0 additions & 77 deletions packages/server/test/unit/util/trash_spec.js

This file was deleted.

74 changes: 74 additions & 0 deletions packages/server/test/unit/util/trash_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import fs from 'fs'
import os from 'os'
import path from 'path'
import trash from '../../../lib/util/trash'
import sinon from 'sinon'
import { expect } from 'chai'

require('../../spec_helper')

// Creates test directories and files for trash testing
const populateDirectories = (basePath: string): void => {
fs.mkdirSync(basePath, { recursive: true })
fs.mkdirSync(path.resolve(basePath, 'bar'), { recursive: true })
fs.mkdirSync(path.resolve(basePath, 'bar', 'baz'), { recursive: true })

fs.writeFileSync(path.resolve(basePath, 'a.txt'), '')
fs.writeFileSync(path.resolve(basePath, 'bar', 'b.txt'), '')
fs.writeFileSync(path.resolve(basePath, 'bar', 'baz', 'c.txt'), '')

expect(fs.existsSync(path.resolve(basePath, 'a.txt'))).to.be.true
expect(fs.existsSync(path.resolve(basePath, 'bar', 'b.txt'))).to.be.true
expect(fs.existsSync(path.resolve(basePath, 'bar', 'baz', 'c.txt'))).to.be.true
}

// Verifies that directories exist but their contents have been removed
const expectDirectoriesExist = (basePath: string): void => {
expect(fs.existsSync(basePath)).to.be.true
expect(fs.existsSync(path.resolve(basePath, 'a.txt'))).to.be.false
expect(fs.existsSync(path.resolve(basePath, 'bar', 'b.txt'))).to.be.false
expect(fs.existsSync(path.resolve(basePath, 'bar', 'baz', 'c.txt'))).to.be.false
}

describe('lib/util/trash', () => {
let tempDir: string

beforeEach(() => {
tempDir = path.join(os.tmpdir(), `cypress-test-${Date.now()}`)
fs.mkdirSync(tempDir, { recursive: true })
})

afterEach(() => {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true })
}
})

context('.folder', () => {
it('trashes contents of directory in non-Linux', async () => {
Copy link
Member Author

@jennifer-shehane jennifer-shehane May 9, 2025

Choose a reason for hiding this comment

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

This test used to use mock-fs, but it would literally fail every time you ran it locally on a mac because the trash code was literally trying to trash a file that never existed, so this has been updated to just make temp directories during the tests outright.

sinon.stub(os, 'platform').returns('darwin')
const basePath = path.join(tempDir, 'foo')

populateDirectories(basePath)

await trash.folder(basePath)
expectDirectoriesExist(basePath)
fs.rmdirSync(basePath)
})

it('doesn\'t fail if directory is non-existent', async () => {
await trash.folder(path.join(tempDir, 'bar'))
})

it('completely removes directory on Linux', async () => {
sinon.stub(os, 'platform').returns('linux')
const basePath = path.join(tempDir, 'foo')

populateDirectories(basePath)

await trash.folder(basePath)
expectDirectoriesExist(basePath)
fs.rmdirSync(basePath)
})
})
})
1 change: 1 addition & 0 deletions scripts/binary/binary-cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const getDependencyPathsToKeep = async (buildAppDir) => {
'node_modules/html-webpack-plugin-5/index.js',
'node_modules/mocha-7.2.0/index.js',
'packages/server/node_modules/webdriver/build/index.js',
'packages/server/node_modules/@wdio/utils/build/node.js',
Copy link
Member Author

Choose a reason for hiding this comment

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

// dependencies needed for geckodriver when running firefox in the binary
'node_modules/pump/index.js',
'node_modules/sprintf-js/src/sprintf.js',
Expand Down
Loading
Loading