trufflesuite / truffle

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
14.02k stars 2.32k forks source link

Unable to run tests explicitly from a selected folder #5066

Open mario-christopher opened 2 years ago

mario-christopher commented 2 years ago

Issue

I want to be able to run Tests from a selected folder. On discussing with the Truffle team, it seems that this is possible using the cmdLine : truffle test .test/phase-3/*.js

When the above cmd is executed from package.json scripts, I get the error : Error: Cannot find module 'D:\<folder-path>\test\phase-3\*.js'

When the above cmd is executed directly, I get no error, but no Tests are executed . Could be a "Windows" specific error ?

Steps to Reproduce

On a Windows machine, create a sub-folder under the test directory, and create your test file in it. Run truffle test .test/phase-3/*.js either from package.json scripts or directly.

Expected Behavior

Tests within the sub-folder alone should execute.

Actual Results

No tests are executed

Environment

swisstackle commented 2 years ago

Reproduced the error. Same error message.

When using the specific filename, it works: i.e: truffle test ./test/testest/PlayersTest.js

With "*.js" it doesnt work.

I've tested it with the commandline, not in package.json scripts.

Truffle version: 5.5.11 Node version: 16.14.0 OS: Windows 10

swisstackle commented 2 years ago

@cds-amal @gnidan @eggplantzzz Should we even consider making "*.js" work? Why don't we just give the user an option to give a directory and then all the files inside the directory are being tested.

See the comment inside else if (inputArgs.length > 0) {}. There we could just check if inputArgs is a directory and if yes, push all the files to filesToRun. If we wanna make it even better, we could even accept multiple directories so that the user can organize his tests better. I could implement this real quick and make a PR.

const determineTestFilesToRun = ({ inputFile, inputArgs = [], config }) => {
  const path = require("path");
  const fs = require("fs");
  const glob = require("glob");
  let filesToRun = [];

  console.log("inputArgs: "+inputArgs);
  if (inputFile) {
    filesToRun.push(inputFile);
  } else if (inputArgs.length > 0) {

   //Check if inputArgs is a directory, if yes, just push all files fo filesToRun

    inputArgs.forEach(inputArg => filesToRun.push(inputArg));
  }

  if (filesToRun.length === 0) {
    const directoryContents = glob.sync(
      `${config.test_directory}${path.sep}**${path.sep}*`
    );
    filesToRun =
      directoryContents.filter(item => fs.statSync(item).isFile()) || [];
  }
  return filesToRun.filter(file => {
    return file.match(config.test_file_extension_regexp) !== null;
  });
};
swisstackle commented 2 years ago

I am working on implementing it at the moment. I am writing test cases for this functionality at the moment. Where do you guys typically locate tests for commands that don't have their own package (such as the test command)?