Skip to content
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

[Bug] glob is broken in Windows and cannot iterate by folder #203

Closed
kkent030315 opened this issue Oct 31, 2022 · 2 comments
Closed

[Bug] glob is broken in Windows and cannot iterate by folder #203

kkent030315 opened this issue Oct 31, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kkent030315
Copy link
Contributor

kkent030315 commented Oct 31, 2022

Problem

Hey, I am having problems loading fixtures from a folder.
The folder ./graphql/fixtures/ actually contains .yml but never be loaded in both CLI & Programmatically loading fixtures

📦fixtures
 ┗ 📜Customer.yml
> npm run fixtures load ./graphql/fixtures/ --dataSource=./dataSource.ts --sync --require=ts-node/register --require=tsconfig-paths/register    

Connection to database...
query: SELECT * FROM current_schema()
query: CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
query: SELECT version();
Loading fixtureConfigs
Resolving fixtureConfigs
Progress [██████████████████████████████████████████████████] 100% | ET 
Database disconnect
loadFixtures('./graphql/fixtures') // never loads any files.

But specifying the exact file just like below works. as well on the Programmatically loading fixtures.

> npm run fixtures load ./graphql/fixtures/Customer.yml --dataSource=./dataSource.ts --sync --require=ts-node/register --require=tsconfig-paths/register
loadFixtures('./graphql/fixtures/Customer.yml') // loads only "Customer.yml" file as expected.

So then I cloned this repo and tried to execute tests:

gh repo clone RobinCK/typeorm-fixtures
npm i
npm run test

Then specific tests regarding Loader, Resolver will be failed.

Output result of `npm run test`
> typeorm-fixtures-cli@3.0.1 test
> nyc mocha "test/**/*.ts"



  Builder
    ✔ should be build entity
    ✔ should be build and transformed entity
    ✔ should be processed entity
    ✔ should be call method
    ✔ should be processor not found
    ✔ should be invalid __call parameter
    ✔ should be resolved entity field as promised

  Loader
    1) should be loaded data from folder
    ✔ should be loaded data from file
    ✔ should be fail load
    ✔ should be no such file
    2) should be invalid fixture config

  Parser
    ✔ should be parsed object
    ✔ should be parsed array
    ✔ should be parsed deep object
    ✔ should be parsed deep array
    ✔ should parse `null` as `null`

  EJS parser
    ✔ should be support
    ✔ should be not support
    ✔ should be parsed

  Faker parser
    ✔ should be support
    ✔ should be not support
    ✔ should be number
    ✔ should be boolean
    ✔ should be string
    ✔ should be translated string

  Parameter parser
    ✔ should be support
    ✔ should be not support
    ✔ should be parsed
    ✔ should be not parsed
    ✔ should be taken from process.env
    ✔ should throw on unknown env variable
    ✔ should prefer explicit parameters to env variables

  Parameter parser
    ✔ should be support
    ✔ should be not support
    ✔ should be resolver
    ✔ should be resolver mask
    ✔ should be not resolver

  Resolver
    3) should be resolved fixtures
    ✔ should be resolved range fixtures
    ✔ should be resolved range reference
    ✔ should be resolved range fixtures
    ✔ should be resolved deep reference
    ✔ should be resolved mask reference
    ✔ should be resolved current
    ✔ should be resolved calculated current
    ✔ should be fail resolved current
    ✔ should be reference not found
    ✔ should be resolved with equal dependencies for wildcard

  Fixtures Iterator
    ✔ should be sort and iterate fixtures


  47 passing (67ms)
  3 failing

  1) Loader
       should be loaded data from folder:

      AssertionError: expected [] to have a length of 2 but got +0
      + expected - actual

      -0
      +2

      at Context.<anonymous> (test\unit\Loader.test.ts:18:28)
      at processImmediate (node:internal/timers:466:21)

  2) Loader
       should be invalid fixture config:
     AssertionError: expected [Function] to throw an error
      at Context.<anonymous> (test\unit\Loader.test.ts:94:92)
      at processImmediate (node:internal/timers:466:21)

  3) Resolver
       should be resolved fixtures:

      AssertionError: expected [] to deeply equal [ { parameters: {}, …(4) }, …(1) ]
      + expected - actual

      -[]
      +[
      +  {
      +    "data": {
      +      "email": "{{internet.email}}"
      +      "firstName": "{{name.firstName}}"
      +      "lastName": "{{name.lastName}}"
      +    }
      +    "dependencies": []
      +    "entity": "User"
      +    "name": "user1"
      +    "parameters": {}
      +  }
      +  {
      +    "data": {
      +      "description": "{{lorem.paragraphs}}"
      +      "title": "{{name.title}}"
      +      "user": "@user1"
      +    }
      +    "dependencies": [
      +      "user1"
      +    ]
      +    "entity": "Post"
      +    "name": "post1"
      +    "parameters": {}
      +  }
      +]

      at Context.<anonymous> (test\unit\Resolver.test.ts:19:32)
      at processImmediate (node:internal/timers:466:21)

Cause

load(fixturesPath: string): void {
const extensions = this.loaders.map((l) => l.extensionSupport.map((e) => e.substr(1)).join(',')).join(',');
let files: string[] = [];
if (fs.lstatSync(fixturesPath).isFile()) {
if (!this.loaders.find((l) => l.isSupport(fixturesPath))) {
throw new Error(`File extension "${path.extname(fixturesPath)}" not support`);
}
files = [fixturesPath];
} else {
files = glob.sync(path.resolve(path.join(fixturesPath, `*.{${extensions}}`)));
}
for (const file of files) {

files was empty array [] so this line of code is cause:

files = glob.sync(path.resolve(path.join(fixturesPath, `*.{${extensions}}`)));

So then, I thought something is wrong in glob and downgraded to "glob": "^7.x", it works.
According to the glob changelog in 8.x

8.0
Only support node v12 and higher
\ is now only used as an escape character, and never as a path separator in glob patterns, so that Windows users have a way to match against filenames containing literal glob pattern characters.
Glob pattern paths must use forward-slashes as path separators, since \ is an escape character to match literal glob pattern characters.
(8.0.2) cwd and root will always be automatically coerced to use / as path separators on Windows, as they cannot contain glob patterns anyway, and are often supplied by path.resolve() and other methods that will use \ path separators by default.

Then I made some changes, to avoid \ path separator and use / instead:

files = glob.sync(
                path
                    .resolve(path.join(fixturesPath, `*.{${extensions}}`))
                    .split(path.sep)
                    .join('/'),
            );

The all tests passed and fixed.
I'll make a PR for this issue, so I appreciate if you take a look to it. :)

Your Environment

Software Version(s)
typeorm-fixtures 3.0.1
Node v16.14.2
npm/Yarn npm
Operating System Windows 11
@kkent030315
Copy link
Contributor Author

I also think adding tests in CI for multiple environments (especially Windows for this case) could catch this sort of environemnt-specific bugs and improve reliability.

@kkent030315
Copy link
Contributor Author

This issue is relared to #199 I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants