Skip to content

Commit

Permalink
Fix edge case where 'env' doesn't exist in a job
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-flinn committed Jan 13, 2024
1 parent 8cf9043 commit d5b0f77
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 9 deletions.
202 changes: 202 additions & 0 deletions lint-workflow-v2/Session.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
silent only
silent tabonly
cd ~/bitwarden/official-repos/gh-actions/lint-workflow-v2
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
set shortmess=aoOA
else
set shortmess=aoO
endif
badd +33 tests/test_lint.py
badd +33 tests/test_utils.py
badd +38 Taskfile.yml
badd +0 src/rules/job_environment_prefix.py
badd +0 tests/rules/test_job_environment_prefix.py
badd +0 tests/fixtures/test_a.yaml
badd +4 src/load.py
argglobal
%argdel
$argadd tests/test_lint.py
set stal=2
tabnew +setlocal\ bufhidden=wipe
tabrewind
edit src/rules/job_environment_prefix.py
let s:save_splitbelow = &splitbelow
let s:save_splitright = &splitright
set splitbelow splitright
wincmd _ | wincmd |
vsplit
1wincmd h
wincmd w
let &splitbelow = s:save_splitbelow
let &splitright = s:save_splitright
wincmd t
let s:save_winminheight = &winminheight
let s:save_winminwidth = &winminwidth
set winminheight=0
set winheight=1
set winminwidth=0
set winwidth=1
exe 'vert 1resize ' . ((&columns * 163 + 163) / 327)
exe 'vert 2resize ' . ((&columns * 163 + 163) / 327)
argglobal
balt tests/rules/test_job_environment_prefix.py
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 70 - ((58 * winheight(0) + 31) / 62)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 70
normal! 018|
wincmd w
argglobal
if bufexists(fnamemodify("tests/rules/test_job_environment_prefix.py", ":p")) | buffer tests/rules/test_job_environment_prefix.py | else | edit tests/rules/test_job_environment_prefix.py | endif
if &buftype ==# 'terminal'
silent file tests/rules/test_job_environment_prefix.py
endif
balt src/rules/job_environment_prefix.py
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 40 - ((32 * winheight(0) + 31) / 62)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 40
normal! 0
wincmd w
exe 'vert 1resize ' . ((&columns * 163 + 163) / 327)
exe 'vert 2resize ' . ((&columns * 163 + 163) / 327)
tabnext
edit src/load.py
let s:save_splitbelow = &splitbelow
let s:save_splitright = &splitright
set splitbelow splitright
wincmd _ | wincmd |
vsplit
wincmd _ | wincmd |
vsplit
2wincmd h
wincmd w
wincmd w
let &splitbelow = s:save_splitbelow
let &splitright = s:save_splitright
wincmd t
let s:save_winminheight = &winminheight
let s:save_winminwidth = &winminwidth
set winminheight=0
set winheight=1
set winminwidth=0
set winwidth=1
exe 'vert 1resize ' . ((&columns * 109 + 163) / 327)
exe 'vert 2resize ' . ((&columns * 108 + 163) / 327)
exe 'vert 3resize ' . ((&columns * 108 + 163) / 327)
argglobal
balt tests/fixtures/test_a.yaml
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 60 - ((26 * winheight(0) + 30) / 61)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 60
normal! 0
wincmd w
argglobal
if bufexists(fnamemodify("tests/fixtures/test_a.yaml", ":p")) | buffer tests/fixtures/test_a.yaml | else | edit tests/fixtures/test_a.yaml | endif
if &buftype ==# 'terminal'
silent file tests/fixtures/test_a.yaml
endif
balt src/load.py
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 8 - ((7 * winheight(0) + 30) / 61)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 8
normal! 0
wincmd w
argglobal
if bufexists(fnamemodify("Taskfile.yml", ":p")) | buffer Taskfile.yml | else | edit Taskfile.yml | endif
if &buftype ==# 'terminal'
silent file Taskfile.yml
endif
balt tests/fixtures/test_a.yaml
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 39 - ((38 * winheight(0) + 30) / 61)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 39
normal! 063|
wincmd w
exe 'vert 1resize ' . ((&columns * 109 + 163) / 327)
exe 'vert 2resize ' . ((&columns * 108 + 163) / 327)
exe 'vert 3resize ' . ((&columns * 108 + 163) / 327)
tabnext 2
set stal=1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let &winminheight = s:save_winminheight
let &winminwidth = s:save_winminwidth
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :
4 changes: 4 additions & 0 deletions lint-workflow-v2/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ tasks:
- pipenv run pytest --cov-report term-missing --cov=src tests

test:e2e:lint:
cmds:
- pipenv run python cli.py -v lint --files tests/fixtures

test:e2e:lint:single:
cmds:
- pipenv run python cli.py -v lint --files tests/fixtures/test.yml

Expand Down
11 changes: 6 additions & 5 deletions lint-workflow-v2/src/rules/job_environment_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ def fn(self, obj: Job) -> Tuple[bool, str]:
"""
correct = True

offending_keys = []
for key in obj.env.keys():
if key[0] != "_":
offending_keys.append(key)
correct = False
if obj.env:
offending_keys = []
for key in obj.env.keys():
if key[0] != "_":
offending_keys.append(key)
correct = False

if correct:
return True, ""
Expand Down
35 changes: 31 additions & 4 deletions lint-workflow-v2/tests/rules/test_job_environment_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,24 @@ def fixture_correct_workflow():
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)


@pytest.fixture(name="incorrect_workflow")
def fixture_incorrect_workflow():
@pytest.fixture(name="no_env_workflow")
def fixture_no_env_workflow():
workflow = """\
---
on:
workflow_dispatch:
jobs:
job-key:
runs-on: ubuntu-22.04
steps:
- run: echo test
"""
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)


@pytest.fixture(name="missing_prefix_workflow")
def fixture_missing_prefix_workflow():
workflow = """\
---
on:
Expand Down Expand Up @@ -61,8 +77,19 @@ def test_rule_on_correct_workflow(rule, correct_workflow):
assert finding is None


def test_rule_on_incorrect_workflow(rule, incorrect_workflow):
obj = incorrect_workflow.jobs["job-key"]
def test_rule_on_no_env_workflow(rule, no_env_workflow):
obj = no_env_workflow.jobs["job-key"]

result, message = rule.fn(no_env_workflow.jobs["job-key"])
assert result is True
assert message == ""

finding = rule.execute(obj)
assert finding is None


def test_rule_on_missing_prefix_workflow(rule, missing_prefix_workflow):
obj = missing_prefix_workflow.jobs["job-key"]

result, message = rule.fn(obj)
assert result is False
Expand Down

0 comments on commit d5b0f77

Please sign in to comment.