dir-compare icon indicating copy to clipboard operation
dir-compare copied to clipboard

`skipSubdirs` still compares the directories themselves

Open hudsonm62 opened this issue 2 years ago • 2 comments

As the title says, skipSubdirs still compares directories themselves. Not sure what the intended functionality is, but I expected it to be ignored all together in the compare.

This extends gliviu/dir-compare-cli#1

File structure

.\compare
│   d_file2.txt
│   m_file1.txt
│   m_file3.txt
│
├───directory1
│       d_file2.txt
│       m_file1.txt
│       m_file3.txt
│
└───directory2
        d_file2.txt
        m_file1.txt
        m_file3.txt

Reproduction Script

const diff = require("dir-compare")

const options = {
  skipSubdirs: true,
}

const input = {
  path1: "./compare/directory1",
  path2: "./compare/",
}

diff
  .compare(input.path1, input.path2, options)
  .then((results) => {
    results.diffSet.forEach((d) => {
      const differenceString = `${d.relativePath}${d.name1} (${d.type1}): ${d.state} - ${d.name2} (${d.type2})`
      console.log(differenceString)
    })
  })
  .catch((error) => {
    throw new Error(error)
  })

Outputs

> node .\test.js

\undefined (missing): right - directory1 (directory)
\undefined (missing): right - directory2 (directory)
\d_file2.txt (file): equal - d_file2.txt (file)     
\m_file1.txt (file): equal - m_file1.txt (file)     
\m_file3.txt (file): equal - m_file3.txt (file)

With skipSubdirs: false

> node .\test.js
\undefined (missing): right - directory1 (directory)
\directory1undefined (missing): right - d_file2.txt (file)
\directory1undefined (missing): right - m_file1.txt (file)
\directory1undefined (missing): right - m_file3.txt (file)
\undefined (missing): right - directory2 (directory)
\directory2undefined (missing): right - d_file2.txt (file)
\directory2undefined (missing): right - m_file1.txt (file)
\directory2undefined (missing): right - m_file3.txt (file)
\d_file2.txt (file): equal - d_file2.txt (file)
\m_file1.txt (file): equal - m_file1.txt (file)
\m_file3.txt (file): equal - m_file3.txt (file)

edit- typo

hudsonm62 avatar Apr 27 '24 04:04 hudsonm62

Thanks for the detailed report. I'll take a look these days.

gliviu avatar Apr 30 '24 19:04 gliviu

Starting with v5.0, skipSubdirs has a different behavior.

Lets consider these directories.

d1
  subdir1
    a3.txt
  a1.txt (size=1)
  a2.txt (size=1)
d2
  subdir1
    a4.txt
  subdir2
    a5.txt
  a1.txt (size=1)
  a2.txt (size=1)

And this code.

import { compareSync } from "dir-compare";
const options = {
  skipSubdirs: true
}
const path1 = "d1"
const path2 = "d2"
const results = compareSync(path1, path2, options)
results.diffSet?.forEach((d) => {
  const differenceString = `${d.relativePath}${d.name1} (${d.type1}): ${d.state} - ${d.name2} (${d.type2})`
  console.log(differenceString)
})

In v5+ the comparison between d1 and d2 using skipSubdirs, correctly ignores any inner directories.

/a1.txt (file): equal - a1.txt (file)
/a2.txt (file): equal - a2.txt (file)

In v4.x the inner directories are included in the comparison, without their content.

/subdir1 (directory): equal - subdir1 (directory)
/undefined (missing): right - subdir2 (directory)
/a1.txt (file): equal - a1.txt (file)
/a2.txt (file): equal - a2.txt (file)

gliviu avatar May 04 '24 20:05 gliviu