-
Notifications
You must be signed in to change notification settings - Fork 446
@clerk/upgrade detect binaries and gitignore during scan #8324
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
Open
rofinn
wants to merge
3
commits into
clerk:main
Choose a base branch
from
rofinn:rf/upgrade-exclude-node-modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,18 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('node:child_process', async importOriginal => { | ||
| const actual = await importOriginal(); | ||
| return { | ||
| ...actual, | ||
| execSync: vi.fn(actual.execSync), | ||
| }; | ||
| }); | ||
|
|
||
| import { execSync, spawnSync } from 'node:child_process'; | ||
|
|
||
| import { loadConfig } from '../../config.js'; | ||
| import { runCodemods, runScans } from '../../runner.js'; | ||
| import { createTempFixture } from '../helpers/create-fixture.js'; | ||
|
|
@@ -22,6 +35,7 @@ vi.mock('../../render.js', () => ({ | |
| promptText: vi.fn((msg, defaultValue) => defaultValue), | ||
| renderCodemodResults: vi.fn(), | ||
| renderText: vi.fn(), | ||
| renderDebug: vi.fn(), | ||
| })); | ||
|
|
||
| describe('runCodemods', () => { | ||
|
|
@@ -219,6 +233,132 @@ describe('runScans', () => { | |
| expect(results[0].instances).toEqual([]); | ||
| }); | ||
|
|
||
| it('scans extensionless node scripts as text files', async () => { | ||
| const config = await loadConfig('nextjs', 6); | ||
| config.changes = [ | ||
| { | ||
| title: 'Test extensionless script', | ||
| matcher: /afterSignInUrl/g, | ||
| packages: ['*'], | ||
| category: 'breaking', | ||
| warning: false, | ||
| docsAnchor: 'test', | ||
| content: 'Test', | ||
| }, | ||
| ]; | ||
|
|
||
| const content = '#!/usr/bin/env node\nafterSignInUrl\n'; | ||
| fs.writeFileSync(path.join(fixture.path, 'run'), content, 'utf8'); | ||
|
|
||
| const results = await runScans(config, 'nextjs', { | ||
| dir: fixture.path, | ||
| ignore: [], | ||
| }); | ||
|
|
||
| const expected = path.relative(process.cwd(), path.join(fixture.path, 'run')); | ||
| expect(results[0].instances.some(instance => instance.file === expected)).toBe(true); | ||
| }); | ||
|
|
||
| it('skips binary files that are not covered by ignore globs', async () => { | ||
| const config = await loadConfig('nextjs', 6); | ||
| config.changes = config.changes.filter(change => change.matcher); | ||
|
|
||
| const binaryPath = path.join(fixture.path, 'binary'); | ||
| fs.writeFileSync(binaryPath, Buffer.from([0x7f, 0x45, 0x4c, 0x46, 0x00])); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this would be a stronger test if I added |
||
|
|
||
| const results = await runScans(config, 'nextjs', { dir: fixture.path, ignore: [] }); | ||
|
|
||
| const allInstances = results.flatMap(result => result.instances); | ||
| const skipped = path.relative(process.cwd(), binaryPath); | ||
| expect(allInstances.every(instance => instance.file !== skipped)).toBe(true); | ||
| }); | ||
|
|
||
| it('respects .gitignore files by default', async () => { | ||
| // Guard as this test assumes git is available | ||
| if (spawnSync('git', ['--version'], { stdio: 'ignore' }).status !== 0) return; | ||
|
|
||
| const config = await loadConfig('nextjs', 6); | ||
| config.changes = config.changes.filter(change => change.matcher); | ||
|
|
||
| const gitPath = fs.mkdtempSync(path.join(fixture.path, 'gitignore-')); | ||
|
|
||
| expect(spawnSync('git', ['init', '-q'], { cwd: gitPath, stdio: 'ignore' }).status).toBe(0); | ||
| fs.writeFileSync(path.join(gitPath, '.gitignore'), 'run\n', 'utf8'); | ||
| fs.writeFileSync(path.join(gitPath, 'run'), '#!/usr/bin/env node\nafterSignInUrl\n', 'utf8'); | ||
|
|
||
| const results = await runScans(config, 'nextjs', { dir: gitPath, ignore: [] }); | ||
|
|
||
| const skipped = path.relative(process.cwd(), path.join(gitPath, 'run')); | ||
| expect(results.flatMap(result => result.instances).every(instance => instance.file !== skipped)).toBe(true); | ||
| }); | ||
|
|
||
| it('can skip using .gitignore files', async () => { | ||
| // Guard as this test assumes git is available | ||
| if (spawnSync('git', ['--version'], { stdio: 'ignore' }).status !== 0) return; | ||
|
|
||
| const config = await loadConfig('nextjs', 6); | ||
| config.changes = config.changes.filter(change => change.matcher); | ||
|
|
||
| const gitPath = fs.mkdtempSync(path.join(fixture.path, 'gitignore-')); | ||
|
|
||
| expect(spawnSync('git', ['init', '-q'], { cwd: gitPath, stdio: 'ignore' }).status).toBe(0); | ||
| fs.writeFileSync(path.join(gitPath, '.gitignore'), 'run\n', 'utf8'); | ||
| fs.writeFileSync(path.join(gitPath, 'run'), '#!/usr/bin/env node\nafterSignInUrl\n', 'utf8'); | ||
|
|
||
| const results = await runScans(config, 'nextjs', { | ||
| dir: gitPath, | ||
| ignore: [], | ||
| skipGitignore: true, | ||
| }); | ||
|
|
||
| const expected = path.relative(process.cwd(), path.join(gitPath, 'run')); | ||
| expect(results.flatMap(result => result.instances).some(instance => instance.file === expected)).toBe(true); | ||
| }); | ||
|
|
||
| it('falls back to normal scanning when git is not installed', async () => { | ||
| const config = await loadConfig('nextjs', 6); | ||
| config.changes = config.changes.filter(change => change.matcher); | ||
|
|
||
| vi.mocked(execSync).mockImplementationOnce(() => { | ||
| const error = new Error('spawnSync git ENOENT'); | ||
| error.code = 'ENOENT'; | ||
| throw error; | ||
| }); | ||
|
|
||
| fs.writeFileSync(path.join(fixture.path, '.gitignore'), 'run\n', 'utf8'); | ||
| fs.writeFileSync(path.join(fixture.path, 'run'), '#!/usr/bin/env node\nafterSignInUrl\n', 'utf8'); | ||
|
|
||
| const results = await runScans(config, 'nextjs', { | ||
| dir: fixture.path, | ||
| ignore: [], | ||
| }); | ||
|
|
||
| const file = path.relative(process.cwd(), path.join(fixture.path, 'run')); | ||
| expect(results.flatMap(result => result.instances).some(instance => instance.file === file)).toBe(true); | ||
| }); | ||
|
|
||
| it('falls back to normal scanning when the directory is not a git repo', async () => { | ||
| const config = await loadConfig('nextjs', 6); | ||
| config.changes = config.changes.filter(change => change.matcher); | ||
|
|
||
| vi.mocked(execSync).mockImplementationOnce(() => { | ||
| const error = new Error('fatal: not a git repository'); | ||
| error.status = 128; | ||
| throw error; | ||
| }); | ||
|
|
||
| fs.writeFileSync(path.join(fixture.path, '.gitignore'), 'run\n', 'utf8'); | ||
| fs.writeFileSync(path.join(fixture.path, 'run'), '#!/usr/bin/env node\nafterSignInUrl\n', 'utf8'); | ||
|
|
||
| const results = await runScans(config, 'nextjs', { | ||
| dir: fixture.path, | ||
| ignore: [], | ||
| }); | ||
|
|
||
| const file = path.relative(process.cwd(), path.join(fixture.path, 'run')); | ||
| expect(results.flatMap(result => result.instances).some(instance => instance.file === file)).toBe(true); | ||
| }); | ||
|
|
||
| it('includes both changes with and without matchers', async () => { | ||
| const config = await loadConfig('nextjs', 6); | ||
| // Add a change without a matcher and one with a matcher | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got a bit lazy while testing. If folks want I can add a concrete config for each test to be more specific?