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

New: Use path to load resources in .sonarwhalrc #1134

Closed
wants to merge 2 commits into from
Closed

New: Use path to load resources in .sonarwhalrc #1134

wants to merge 2 commits into from

Conversation

molant
Copy link
Member

@molant molant commented Jun 13, 2018

Enable loading a resource using a path instead of the ID of the package.
This is useful when you have multiple configuration files with some
common parts or when you have a locally developed rule that you don't
want to publish on a package.


Fix #1123

Pull request checklist

Make sure you:

For non-trivial changes, please make sure you also:

  • Added/Updated related documentation.
  • Added/Updated related tests.

Short description of the change(s)

@molant
Copy link
Member Author

molant commented Jun 13, 2018

Going to add tests and change some stuff in rule-runner, but you can take a look at this. If we are ok with this approach then #1125 can be closed.

@molant
Copy link
Member Author

molant commented Jun 13, 2018

Pending things:

  • Update documentation to explain how to things work with a path
  • Update rule-runner to use a path so the release script works OK
  • Add tests

@@ -244,11 +245,13 @@ const generateConfigPathsToResources = (configurations: Array<string>, name: str
*/
export const loadResource = (name: string, type: ResourceType, configurations: Array<string> = [], verifyVersion = false) => {
debug(`Searching ${name}…`);

const isSource = fs.existsSync(name); // eslint-disable-line no-sync
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if name is a relative path, this is not going to work, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was working on my local tests. IIRC fs. exists uses process.cwd() so it should be ok?
I guess the problem is if we are calling from another place different place than where the .sonarwhalrc file is located (e.g.: when using the file in the user's home folder)

const resourceName = nameSplitted[1] || packageName;
const resourceName = isSource ?
name :
nameSplitted[1] || packageName;

const key: string = `${type}-${name}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key should be something like

const key: string = isSource ? name : `${type}-${name}`

@@ -322,7 +334,12 @@ export const loadResource = (name: string, type: ResourceType, configurations: A
throw new ResourceError(`Resource ${name} not found`, ResourceErrorStatus.NotFound);
}

resources.set(key, resource);
if (isSource) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will no longer need this if you do the key change

@@ -143,15 +143,15 @@ test.serial(`getRuleName - returns an empty string if it can't determine the rul
t.deepEqual(ruleName, '');
});

test.serial(`getRuleName - returns a combined string if a package name is passed`, (t) => {
const sandbox = sinon.createSandbox();
// test.serial(`getRuleName - returns a combined string if a package name is passed`, (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncomment or remove it :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to rewrite all the getRuleName tests. None work after this and some of the previous logic didn't make any sense.

@sarvaje
Copy link
Contributor

sarvaje commented Jun 14, 2018

@molant should I close #1125 in favor of this PR?

@molant
Copy link
Member Author

molant commented Jun 15, 2018

TL;DR: this is going to take longer than I expected if we want to do it right

Longer version:
Supporting paths in the initial configuration is relatively easy (the current code should handle it). The problem is if we want to support paths in "extended" configurations and those configurations are in different paths.
Resources should be loaded relatively to the config that declares them. The way I'm currently working on is to change that to absolute paths but I'll have to change more things in UserConfig and SonarwhalConfig. Problem is that UserConfig knows about paths but not extended configuration and SonarwhalConfig is the opposite, so I'll have to do a few changes around both 😔

@molant
Copy link
Member Author

molant commented Jun 16, 2018

New push that loads the path resources relatively to where the configuration file is.

Enable loading a resource using a path instead of the ID of the package.
This is useful when you have multiple configuration files with some
common parts or when you have a locally developed rule that you don't
want to publish on a package.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Fix #1123
Rename method and resolve relatively to the config

- - - - - - - - - - - - - - - -

Ref #1134
@molant
Copy link
Member Author

molant commented Jun 19, 2018

Closing in favor of #1140 that should be ready to review 😊

@molant molant closed this Jun 19, 2018
alrra pushed a commit that referenced this pull request Jun 21, 2018
@alrra alrra deleted the feature/config-path branch June 21, 2018 17:58
alrra pushed a commit that referenced this pull request Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for paths to resources on .sonarwhalrc
2 participants