Skip to content

Conversation

MohamedLamineAllal
Copy link

Fix issue #10

Fix

  • Fixed reldir not working as it say in the doc

Tests

update + fix: tests

Fixing the tests

  • fixed the tests. Because they were not pure. Before applying my changes. You run the test once they succeed.at others fail. fully randomly.
    • The supposition that the files are processed in a sorted manner is wrong.

    • The glob package does have them alphabetically sorted. While listr package task runner. Does screw that. (I logged glob matched files. it's always coming in a sorted manner. And always the same. And listr when does run it, the order is not guaranteed.)

    • To fix the issue. I opted for tests without relying on order.

    • The were two cases

      • the placeholders, where i need to check against the output list of all placeholders. For this i built a parser. That construct a map by path. I checked and tested that all the paths that are tested are part of the result. So the paths are checked correctly. Then for every path. I use the map to rigorously and without having to rely on any order. Now the tests are pure. And fully stable

        • Checking the paths
	# Check the paths
	[
		' test/samples/sass/css/foldr.css ',
		' test/samples/sass/css/foldr.css/sub.css ',
		' test/samples/sass/css/main.copy.css ',
		' test/samples/sass/css/main.css '
	].forEach (path) -> expect(result.includes(path)).to.be.truthy

                       ▫️ Rest of checks

	# We are using mapping to make tests pure, as Listr run doesn't gurantee the order of execution
	expectedResults = [
		# folder file match
		{
			path: 'test/samples/sass/css/foldr.css',
			name: 'foldr',
			ext: '.css',
			base: 'foldr.css',
			reldir: '',
			dir: "#{process.cwd()}/test/samples/sass/css" # because a folder
		},
		# ✨ Nested folder, and reldir
		{
			path: 'test/samples/sass/css/foldr.css/sub.css',
			name: 'sub',
			ext: '.css',
			base: 'sub.css',
			reldir: 'foldr.css',
			dir: "#{process.cwd()}/test/samples/sass/css/foldr.css"
		},
		{
			path: 'test/samples/sass/css/main.copy.css',
			name: 'main.copy',
			ext: '.css',
			base: 'main.copy.css',
			reldir: '',
			dir: "#{process.cwd()}/test/samples/sass/css"
		},
		{
			path: 'test/samples/sass/css/main.css',
			name: 'main',
			ext: '.css',
			base: 'main.css',
			reldir: '',
			dir: "#{process.cwd()}/test/samples/sass/css"
		}
	]

	for expected in expectedResults
		m = parsedResult.linesMap[expected.path]
		expect(m.name).to.equal expected.name
		expect(m.ext).to.equal expected.ext
		expect(m.base).to.equal expected.base
		expect(m.reldir).to.equal expected.reldir
		expect(m.path).to.equal expected.path
		expect(m.dir).to.equal expected.dir
  • The second type is the test with find. The logic is if we check the length. And I have a number of paths equal to that of the length. If I check that each path equals one of the lines. Then if all is found in one of the lines. That implies each line is unique. And we have that number of paths.
expect(resultLines.length).to.equal 3
expect(resultLines[0]).to.equal 'foldr.css'
expect(resultLines[1]).to.equal 'main.copy.css'
expect(resultLines[2]).to.equal 'main.css'

to

expect(resultLines.find (line) -> line == 'foldr.css').to.be.truthy
expect(resultLines.find (line) -> line == 'main.copy.css').to.be.truthy
expect(resultLines.find (line) -> line == 'main.css').to.be.truthy

This way the tests are fully pure. And always will give the same result.

Updating the test for placeholders

  • Updated the tests of placeholders. To reflect the new changes. I switched

- Should match all until reaching * (so [^\*])
- fixed the tests. Because they were not pure. Before applying my changes. You run once it succeed. you run another it fail. Its random.
    - The supposition  that the files are processed in a sorted manner is wrong.
    - glob package does have them alphabetically sorted. While listr package task runner. Does screw that.
  - To fix the issue. I opted for tests without relying on order.
  - More details on PR danielkalen#11 , fix of issue danielkalen#10
- Updated the tests of placeholders. To reflect the new changes.
@Kristinita
Copy link

Status: Doesn’t work for Windows

1. Summary

@MohamedLamineAllal, foreach-cli — is cross-platform. Your changes don’t work on Windows. Please fix it.

2. MCVE

2.1. Directory structure

2.1.1. Initial
C:\PROJECTS\SASHATRAVIS\OUTPUT
\---theme
    \---stylus
        +---KiraFirstFolder
        |       KiraFirstFile.styl
        |
        \---KiraSecondFolder
            |   KiraSecondFile.styl
            |
            \---KiraSubfolder
                    KiraThirdFile.styl

All .styl files have no content or have valid Stylus markup.

2.1.2. Desired
C:\PROJECTS\SASHATRAVIS\OUTPUT
\---theme
    +---css
    |   +---KiraFirstFolder
    |   |       KiraFirstFile.css
    |   |       
    |   \---KiraSecondFolder
    |       |   KiraSecondFile.css
    |       |   
    |       \---KiraSubfolder
    |               KiraThirdFile.css
    |               
    \---stylus
        +---KiraFirstFolder
        |       KiraFirstFile.styl
        |       
        \---KiraSecondFolder
            |   KiraSecondFile.styl
            |   
            \---KiraSubfolder
                    KiraThirdFile.styl

This structure can be obtained by running the commands:

# [INFO] Stylus CLI usage:
# https://stylus-lang.com/docs/executable.html#compiling-files-example
- stylus output/theme/stylus/KiraFirstFolder/KiraFirstFile.styl --out output/theme/css/KiraFirstFolder/KiraFirstFile.css
- stylus output/theme/stylus/KiraSecondFolder/KiraSecondFile.styl --out output/theme/css/KiraSecondFolder/KiraSecondFile.css
- stylus output/theme/stylus/KiraSecondFolder/KiraSubfolder/KiraThirdFile.styl --out output/theme/css/KiraSecondFolder/KiraSubfolder/KiraThirdFile.css

2.2. Command

As I understand from issue #10 and from your Medium article “Npm scripts - running commands with globs expansion for each file”, the command to get the desired directory structure should be like this:

foreach --glob "output/theme/stylus/**/*.styl" --execute "stylus #{path} --out output/theme/css/#{reldir}/#{name}.css"

2.3. Behavior

2.3.1. Expected — Ubuntu

Travis build:

Output output/theme/stylus/KiraSecondFolder/KiraSecondFile.styl

  compiled output/theme/css/KiraSecondFolder/KiraSecondFile.css

Output output/theme/stylus/KiraFirstFolder/KiraFirstFile.styl

  compiled output/theme/css/KiraFirstFolder/KiraFirstFile.css

Output output/theme/stylus/KiraSecondFolder/KiraSubfolder/KiraThirdFile.styl

  compiled output/theme/css/KiraSecondFolder/KiraSubfolder/KiraThirdFile.css
2.3.2. Unexpected — Windows

AppVeyor build:

foreach --glob "output/theme/stylus/**/*.styl" --execute "stylus #{path} --out output/theme/css/#{reldir}/#{name}.css"
 × Executing command: output/theme/stylus/KiraFirstFolder/KiraFirstFile.styl
 × Executing command: output/theme/stylus/KiraSecondFolder/KiraSecondFile.styl
 × Executing command: output/theme/stylus/KiraSecondFolder/KiraSubfolder/KiraThirdFile.styl
Error output/theme/stylus/KiraSecondFolder/KiraSecondFile.styl
node:fs:1374
  const result = binding.mkdir(
                         ^
Error: ENOENT: no such file or directory, mkdir 'C:\projects\sashatravis\output\theme\css\C:\projects\sashatravis\output\theme\stylus\KiraSecondFolder'
    at mkdirSync (node:fs:1374:26)
    at writeFile (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:738:3)
    at Array.<anonymous> (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:687:13)
    at Renderer.render (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\lib\renderer.js:98:29)
    at C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:678:15
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read/context:68:3) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'mkdir',
  path: 'C:\\projects\\sashatravis\\output\\theme\\css\\C:\\projects\\sashatravis\\output\\theme\\stylus\\KiraSecondFolder'
}
Node.js v21.6.1
Error output/theme/stylus/KiraFirstFolder/KiraFirstFile.styl
node:fs:1374
  const result = binding.mkdir(
                         ^
Error: ENOENT: no such file or directory, mkdir 'C:\projects\sashatravis\output\theme\css\C:\projects\sashatravis\output\theme\stylus\KiraFirstFolder'
    at mkdirSync (node:fs:1374:26)
    at writeFile (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:738:3)
    at Array.<anonymous> (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:687:13)
    at Renderer.render (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\lib\renderer.js:98:29)
    at C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:678:15
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read/context:68:3) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'mkdir',
  path: 'C:\\projects\\sashatravis\\output\\theme\\css\\C:\\projects\\sashatravis\\output\\theme\\stylus\\KiraFirstFolder'
}
Node.js v21.6.1
Error output/theme/stylus/KiraSecondFolder/KiraSubfolder/KiraThirdFile.styl
node:fs:1374
  const result = binding.mkdir(
                         ^
Error: ENOENT: no such file or directory, mkdir 'C:\projects\sashatravis\output\theme\css\C:\projects\sashatravis\output\theme\stylus\KiraSecondFolder\KiraSubfolder'
    at mkdirSync (node:fs:1374:26)
    at writeFile (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:738:3)
    at Array.<anonymous> (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:687:13)
    at Renderer.render (C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\lib\renderer.js:98:29)
    at C:\Users\appveyor\AppData\Roaming\npm\node_modules\stylus\bin\stylus:678:15
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read/context:68:3) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'mkdir',
  path: 'C:\\projects\\sashatravis\\output\\theme\\css\\C:\\projects\\sashatravis\\output\\theme\\stylus\\KiraSecondFolder\\KiraSubfolder'
}
Node.js v21.6.1

2.4. Details

You can see more details about my MCVE in CI builds:

  1. AppVeyor CI build on Windows Server 2019
  2. Travis CI build on Ubuntu Jammy Jellyfish 22.04.3 LTS

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants