Skip to content

Commit

Permalink
Merge pull request #19 from mschuett/fix/reference-scalarnode
Browse files Browse the repository at this point in the history
Fix data types in new !reference handling
  • Loading branch information
mschuett authored Oct 7, 2024
2 parents f1b93f0 + 8a9e85b commit 08537c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test-input/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ html:
# shellcheck will check lower case variables,
# but ignores all upper case variables
- echo "$FOOBAR"
- echo "$FOO"
- echo $FOO
- !reference [.setup, script]
artifacts:
name: "${CI_PROJECT_NAME}-${CI_JOB_NAME}-${CI_COMMIT_SHA}"
Expand Down
2 changes: 2 additions & 0 deletions test-input/.gitlab-ci.yml.test_expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SC2086
SC2086
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# very simple shell loop, I should probably rewrite this in pytest...

GLOBIGNORE=".."
for f in test-input/*.y*ml; do
for f in test-input/.*.y*ml test-input/*.y*ml; do
echo "== test file $f"
if [ ! -f "$f.test_expected" ]; then
echo "ERROR: missing test spec file $f.test_expected"
Expand Down
18 changes: 11 additions & 7 deletions yaml_shellcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys

from ruamel.yaml import YAML
from ruamel.yaml.nodes import ScalarNode

global logger

Expand Down Expand Up @@ -373,24 +374,27 @@ def read_yaml_file(filename):
class GitLabReference(object):
yaml_tag = "!reference"

def __init__(self, elements):
def __init__(self, elements: list[str]):
self.elements = elements

def __str__(self):
return f"# {self.yaml_tag}{','.join(self.elements)}"
return f"# {self.yaml_tag}[{', '.join(self.elements)}]"

@classmethod
def to_yaml(cls, representer, node):
return representer.represent_scalar(
cls.yaml_tag, f"[{', '.join(self.elements)}]"
cls.yaml_tag, f"[{', '.join(node.value)}]"
)

@classmethod
def from_yaml(cls, constructor, node):
if not all(isinstance(element, str) for element in node.value):
raise ValueError(f"Tag {cls.yaml_tag} only support a sequence of strings")

return str(cls(node.value))
if not all(isinstance(element, ScalarNode) for element in node.value):
raise ValueError(
f"Tag {cls.yaml_tag} only support a sequence of ScalarNode "
f"(should all be strings), but found "
f"{[type(element) for element in node.value]}")
# we instantiate a GitLabReference with cls, but return its string representation
return str(cls([element.value for element in node.value]))

yaml = YAML(typ="safe")
yaml.register_class(GitLabReference)
Expand Down

0 comments on commit 08537c9

Please sign in to comment.