Skip to content

Commit

Permalink
Made transform-globs tests dependent on the currently running platform
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim.ryabov committed Dec 1, 2024
1 parent e38b7d1 commit 8395a9b
Showing 1 changed file with 145 additions and 135 deletions.
280 changes: 145 additions & 135 deletions packages/steiger/src/models/config/transform-globs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,11 @@
import { describe, expect, it, vi } from 'vitest'
import { PlatformPath } from 'node:path'
import { resolve as resolveWindows } from 'node:path/win32'
import { resolve as resolveUnix } from 'node:path/posix'
import { describe, expect, it } from 'vitest'
import { Config, Rule } from '@steiger/types'
import { transformGlobs } from './transform-globs'

let globalPlatform = ''

vi.mock('path', async (importOriginal) => {
const actual: PlatformPath = await importOriginal()
return {
...actual,
resolve: vi.fn((...args) => {
return globalPlatform === 'windows' ? resolveWindows(...args) : resolveUnix(...args)
}),
}
})
const platform = process.platform

describe('transformGlobs', () => {
it.each([
{
platform: 'unix',
configLocationPath: '/projects/dummy-project',
expectedGlobs: [
{
ignores: ['/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['/projects/dummy-project/src/shared/ui/index.ts'],
},
],
},
{
platform: 'windows',
configLocationPath: 'C:/projects/dummy-project',
expectedGlobs: [
{
ignores: ['C:/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['C:/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['C:/projects/dummy-project/src/shared/ui/index.ts'],
},
],
},
])(`should convert relative globs to absolute on $platform`, ({ platform, configLocationPath, expectedGlobs }) => {
globalPlatform = platform
it.runIf(platform === 'linux')(`should convert relative globs to absolute`, () => {
const config: Config<Array<Rule>> = [
{
ignores: ['./src/entities/**'],
Expand All @@ -66,44 +19,77 @@ describe('transformGlobs', () => {
},
]

expect(transformGlobs(config, configLocationPath)).toEqual(expectedGlobs)
expect(transformGlobs(config, '/projects/dummy-project')).toEqual([
{
ignores: ['/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['/projects/dummy-project/src/shared/ui/index.ts'],
},
])
})

it.each([
{
platform: 'unix',
configLocationPath: '/projects/dummy-project',
expectedGlobs: [
{
ignores: ['/projects/dummy-project/src/entities', '/projects/dummy-project/**/shared'],
it.runIf(platform === 'win32')(`should convert relative globs to absolute`, () => {
const config: Config<Array<Rule>> = [
{
ignores: ['./src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
{
rules: {
rule1: 'warn',
},
files: ['/projects/dummy-project/src/shared/ui', '/projects/dummy-project/**/pages'],
ignores: ['/projects/dummy-project/src/shared/ui/index.ts'],
files: ['./src/shared/ui/**/*'],
ignores: ['./src/shared/ui/index.ts'],
},
]

expect(transformGlobs(config, 'C:/projects/dummy-project')).toEqual([
{
ignores: ['C:/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
],
},
{
platform: 'windows',
configLocationPath: 'C:/projects/dummy-project',
expectedGlobs: [
{
ignores: ['C:/projects/dummy-project/src/entities', 'C:/projects/dummy-project/**/shared'],
files: ['C:/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['C:/projects/dummy-project/src/shared/ui/index.ts'],
},
])
})

it.runIf(platform === 'linux')('should strip trailing slashes', () => {
const config: Config<Array<Rule>> = [
{
ignores: ['./src/entities/', '**/shared/'],
},
{
rules: {
rule1: 'warn',
},
{
rules: {
rule1: 'warn',
},
files: ['C:/projects/dummy-project/src/shared/ui', 'C:/projects/dummy-project/**/pages'],
ignores: ['C:/projects/dummy-project/src/shared/ui/index.ts'],
files: ['./src/shared/ui/', '**/pages/'],
ignores: ['./src/shared/ui/index.ts'],
},
]

expect(transformGlobs(config, '/projects/dummy-project')).toEqual([
{
ignores: ['/projects/dummy-project/src/entities', '/projects/dummy-project/**/shared'],
},
{
rules: {
rule1: 'warn',
},
],
},
])('should strip trailing slashes on $platform', ({ platform, configLocationPath, expectedGlobs }) => {
globalPlatform = platform
files: ['/projects/dummy-project/src/shared/ui', '/projects/dummy-project/**/pages'],
ignores: ['/projects/dummy-project/src/shared/ui/index.ts'],
},
])
})

it.runIf(platform === 'win32')('should strip trailing slashes', () => {
const config: Config<Array<Rule>> = [
{
ignores: ['./src/entities/', '**/shared/'],
Expand All @@ -117,46 +103,54 @@ describe('transformGlobs', () => {
},
]

expect(transformGlobs(config, configLocationPath)).toEqual(expectedGlobs)
expect(transformGlobs(config, 'C:/projects/dummy-project')).toEqual([
{
ignores: ['C:/projects/dummy-project/src/entities', 'C:/projects/dummy-project/**/shared'],
},
{
rules: {
rule1: 'warn',
},
files: ['C:/projects/dummy-project/src/shared/ui', 'C:/projects/dummy-project/**/pages'],
ignores: ['C:/projects/dummy-project/src/shared/ui/index.ts'],
},
])
})

it.each([
{
platform: 'unix',
configLocationPath: '/projects/dummy-project',
expectedGlobs: [
it.runIf(platform === 'linux')(
"should correctly transform globs that are relative but don't start with a dot",
() => {
const config: Config<Array<Rule>> = [
{
ignores: ['/projects/dummy-project/src/entities/**'],
ignores: ['src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['/projects/dummy-project/src/shared/ui/index.ts'],
files: ['src/shared/ui/**/*'],
ignores: ['src/shared/ui/index.ts'],
},
],
},
{
platform: 'windows',
configLocationPath: 'C:/projects/dummy-project',
expectedGlobs: [
]

expect(transformGlobs(config, '/projects/dummy-project')).toEqual([
{
ignores: ['C:/projects/dummy-project/src/entities/**'],
ignores: ['/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['C:/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['C:/projects/dummy-project/src/shared/ui/index.ts'],
files: ['/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['/projects/dummy-project/src/shared/ui/index.ts'],
},
],
])
},
])(
"should correctly transform globs that are relative but don't start with a dot on $platform",
({ platform, configLocationPath, expectedGlobs }) => {
globalPlatform = platform
)

it.runIf(platform === 'win32')(
"should correctly transform globs that are relative but don't start with a dot",
() => {
const config: Config<Array<Rule>> = [
{
ignores: ['src/entities/**'],
Expand All @@ -170,45 +164,50 @@ describe('transformGlobs', () => {
},
]

expect(transformGlobs(config, configLocationPath)).toEqual(expectedGlobs)
},
)

it.each([
{
platform: 'unix',
configLocationPath: '/projects/dummy-project',
expectedGlobs: [
expect(transformGlobs(config, 'C:/projects/dummy-project')).toEqual([
{
ignores: ['!/projects/dummy-project/src/entities/**'],
ignores: ['C:/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['!/projects/dummy-project/**/shared/ui/**/*'],
ignores: ['!/projects/dummy-project/src/shared/ui/index.ts'],
files: ['C:/projects/dummy-project/src/shared/ui/**/*'],
ignores: ['C:/projects/dummy-project/src/shared/ui/index.ts'],
},
],
])
},
{
platform: 'windows',
configLocationPath: 'C:/projects/dummy-project',
expectedGlobs: [
{
ignores: ['!C:/projects/dummy-project/src/entities/**'],
)

it.runIf(platform === 'linux')('should correctly transform negated globs', () => {
const config: Config<Array<Rule>> = [
{
ignores: ['!src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
{
rules: {
rule1: 'warn',
},
files: ['!C:/projects/dummy-project/**/shared/ui/**/*'],
ignores: ['!C:/projects/dummy-project/src/shared/ui/index.ts'],
files: ['!**/shared/ui/**/*'],
ignores: ['!src/shared/ui/index.ts'],
},
]

expect(transformGlobs(config, '/projects/dummy-project')).toEqual([
{
ignores: ['!/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
],
},
])('should correctly transform negated globs on $platform', ({ platform, configLocationPath, expectedGlobs }) => {
globalPlatform = platform
files: ['!/projects/dummy-project/**/shared/ui/**/*'],
ignores: ['!/projects/dummy-project/src/shared/ui/index.ts'],
},
])
})

it.runIf(platform === 'win32')('should correctly transform negated globs', () => {
const config: Config<Array<Rule>> = [
{
ignores: ['!src/entities/**'],
Expand All @@ -222,6 +221,17 @@ describe('transformGlobs', () => {
},
]

expect(transformGlobs(config, configLocationPath)).toEqual(expectedGlobs)
expect(transformGlobs(config, 'C:/projects/dummy-project')).toEqual([
{
ignores: ['!C:/projects/dummy-project/src/entities/**'],
},
{
rules: {
rule1: 'warn',
},
files: ['!C:/projects/dummy-project/**/shared/ui/**/*'],
ignores: ['!C:/projects/dummy-project/src/shared/ui/index.ts'],
},
])
})
})

0 comments on commit 8395a9b

Please sign in to comment.