tracehubpm / code-review-action

Quality of Code Review Checker, plugin it as GitHub Action
MIT License
7 stars 2 forks source link

Right approach to test system env variables #102

Open l3r8yJ opened 6 months ago

l3r8yJ commented 6 months ago

@h1alexbel take a look, please

I have a class

public final class SkipAuthors implements Scalar<Collection<String>> {

    public SkipAuthors() {
        this(
            new ListOf<>(
                System.getenv().get("SKIP_AUTHORS").split(",")
            )
        );
    }

    @Override
    public Collection<String> value() {
        return Collections.unmodifiableCollection(this.authors);
    }
}

which is taking values of SKIP_AUTHORS sys env, what is the right way to test this? For now test looks like:

final class SkipAuthorsTest {

    @Test
    void worksAsImmutable() throws Exception {
        final Scalar<Collection<String>> authors = new SkipAuthors("ruby", "jeff");
        final Collection<String> expected = new ListOf<>("ruby", "jeff");
        MatcherAssert.assertThat(
            "%s should be equal to %s".formatted(authors.value(), expected),
            expected,
            Matchers.everyItem(
                Matchers.in(authors.value())
            )
        );
    }
}

btw, might be you know how array variables from action.yaml maps to system environment variables

h1alexbel commented 6 months ago

@l3r8yJ it should be INPUT_SKIP_AUTHORS for skip_authors in action.yml

l3r8yJ commented 6 months ago

@h1alexbel yes, but how value will be presented? As far as I know, sys env contains just a string, but when we have something like

skip_authors:
  - l3r8yJ
  - h1alexbel

or

skip_authors: ["l3r8yJ", "h1alexbel"]

we will see something like INPUT_SKIP_AUTHORS=l3r8yJ,h1alexbel or what?

h1alexbel commented 6 months ago

@l3r8yJ good question, I don't really test the first variant before, but for the second one, it should be like this: INPUT_SKIP_AUTHORS='["l3r8yJ", "h1alexbel"]', so convert array into the string, skip_authors values should look like this:

skip_authors: '["l3r8yJ", "h1alexbel"]'

checkout this too: https://stackoverflow.com/a/54568163/19147117