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

Walk runners dir #416

Merged
merged 11 commits into from
Mar 12, 2023
Merged

Walk runners dir #416

merged 11 commits into from
Mar 12, 2023

Conversation

brunomgalmeida
Copy link
Contributor

This provides a better way to organize things when we have many scripts and runners

@bugy
Copy link
Owner

bugy commented Mar 24, 2021

Hi @brunomgalmeida thanks for the PR! I think this is a nice feature

Would you mind adding a test for it, please? In tests.config_service_test.ConfigServiceTest

@bugy bugy added the feature label Mar 24, 2021
@bugy bugy added this to the 1.17.0 milestone Mar 24, 2021
@brunomgalmeida
Copy link
Contributor Author

thanks @bugy
I am having some difficulty understanding the tests parts.

Would you be able to help me writing the test for this one, and I should be able to write tests from here onwards.

How can I run the tests?

@bugy
Copy link
Owner

bugy commented Mar 30, 2021

Hi @brunomgalmeida you could run the tests from src folder. The command is:
python3 -m unittest discover -s tests -p "*.py" -t .
It will run all the tests. I believe by replacing "*.py" with a specific filename you can run only a single file

Regarding the test, you might copy test_list_configs_when_multiple test method and adapt it to your changes:

    def test_list_configs_when_multiple_and_subfolders(self):
        _create_script_config_file('conf_x', subfolder = 's1')
        _create_script_config_file('conf_y', subfolder = 's2')
        _create_script_config_file('A B C', subfolder = os.path.join('s1', 'inner'))

        configs = self.config_service.list_configs(self.user)
        conf_names = [config.name for config in configs]
        self.assertCountEqual(['conf_x', 'conf_y', 'A B C'], conf_names)

src/tests/config_service_test.py Outdated Show resolved Hide resolved
# Read config file from within directories too
for _root, _dirs, _files in os.walk(configs_dir, topdown=True):
for name in _files:
files.append( re.sub ( r'^.*/runners/', '', os.path.join( _root, name) ) )
Copy link
Owner

Choose a reason for hiding this comment

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

I think you can just append name here. It doesn't include parent path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, simply adding the name returns different results and causes the tests to fail

Copy link
Owner

Choose a reason for hiding this comment

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

Alright, true. I just wanted to avoid having forward slashes for windows compatibility.
In this case I would suggest to files.append(os.path.join(_root, name))

And later, in the line 218, don't do os.path.join(configs_dir, config_path) and use config_path directly

PS could you change the formatting rules please, and remove whitespaces in parentheses? E.g. ( a, b, c ) => (a, b, c) It would be better to have the same style across the project, if possible

@bugy bugy removed this from the 1.17.0 milestone May 22, 2021
Copy link
Owner

@bugy bugy left a comment

Choose a reason for hiding this comment

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

Hey, thanks for changes, I have only 1 comment, concerning forward slashes in the path

# Read config file from within directories too
for _root, _dirs, _files in os.walk(configs_dir, topdown=True):
for name in _files:
files.append( re.sub ( r'^.*/runners/', '', os.path.join( _root, name) ) )
Copy link
Owner

Choose a reason for hiding this comment

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

Alright, true. I just wanted to avoid having forward slashes for windows compatibility.
In this case I would suggest to files.append(os.path.join(_root, name))

And later, in the line 218, don't do os.path.join(configs_dir, config_path) and use config_path directly

PS could you change the formatting rules please, and remove whitespaces in parentheses? E.g. ( a, b, c ) => (a, b, c) It would be better to have the same style across the project, if possible

@brunomgalmeida
Copy link
Contributor Author

brunomgalmeida commented Jul 20, 2021

  1. The problem with using
files.append(os.path.join(_root, name))

is that the path returned includes the config+runners directory

FileNotFoundError: [Errno 2] No such file or directory: '../testconf/testconf/runners/Script2/Script2.json'

Hence the funky regular expression to remove it. If your only concern is the Windows paths we can update the regex to

files.append( re.sub ( r'^.*(/|\\)runners(/|\\)', '', os.path.join( _root, name) ) )
  1. "PS could you change the formatting rules please"
    Noted

@bugy
Copy link
Owner

bugy commented Jul 21, 2021

Let me check it later today

@bugy
Copy link
Owner

bugy commented Jul 24, 2021

I changed the method this way:

    def _visit_script_configs(self, visitor):
        configs_dir = self._script_configs_folder

        files=[]
        # Read config file from within directories too
        for _root, _dirs, _files in os.walk(configs_dir, topdown=True):
            for name in _files:
                files.append(os.path.join(_root, name))

        configs = [file for file in files if file.lower().endswith(".json")]

        result = []

        for path in configs:
            try:
                content = file_utils.read_file(path)

                visit_result = visitor(path, content)
                if visit_result is not None:
                    result.append(visit_result)

            except StopIteration as e:
                if e.value is not None:
                    result.append(e.value)

            except:
                LOGGER.exception("Couldn't read the file: " + path)

        return result

For me both tests and real server runtime works. For the following structure:
image

@bugy bugy added this to the 1.18.0 milestone Mar 11, 2023
@bugy bugy added the resolved label Mar 11, 2023
@bugy
Copy link
Owner

bugy commented Mar 11, 2023

Merged to master

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.

3 participants