Skip to content

Commit

Permalink
Merge pull request #27 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 2.4.0
  • Loading branch information
andyone authored Jul 9, 2019
2 parents 05bab11 + c48a8ab commit 674100f
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: go
go:
- 1.10.x
- 1.11.x
- 1.12.x
- tip

os:
Expand Down
47 changes: 47 additions & 0 deletions check/checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func getCheckers() []Checker {
checkForIndentInFilesSection,
checkForSetupArguments,
checkForEmptyLinesAtEnd,
checkBashLoops,
}
}

Expand Down Expand Up @@ -644,6 +645,52 @@ func checkForEmptyLinesAtEnd(s *spec.Spec) []Alert {
return nil
}

// checkBashLoops checks bash loops format
func checkBashLoops(s *spec.Spec) []Alert {
if len(s.Data) == 0 {
return nil
}

var result []Alert

sections := []string{
spec.SECTION_BUILD,
spec.SECTION_CHECK,
spec.SECTION_INSTALL,
spec.SECTION_POST,
spec.SECTION_POSTTRANS,
spec.SECTION_POSTUN,
spec.SECTION_PRE,
spec.SECTION_PREP,
spec.SECTION_PRETRANS,
spec.SECTION_PREUN,
spec.SECTION_SETUP,
spec.SECTION_TRIGGERIN,
spec.SECTION_TRIGGERPOSTUN,
spec.SECTION_TRIGGERUN,
spec.SECTION_VERIFYSCRIPT,
}

for _, section := range s.GetSections(sections...) {
for _, line := range section.Data {
lineText := strings.TrimLeft(line.Text, "\t ")

if !strings.HasPrefix(lineText, "for") && !strings.HasPrefix(lineText, "while") {
continue
}

nextLine := s.GetLine(line.Index + 1)
nextLineText := strings.TrimLeft(nextLine.Text, "\t ")

if !strings.HasSuffix(strings.Trim(nextLineText, " "), ";do") && nextLineText == "do" {
result = append(result, Alert{LEVEL_NOTICE, "Place 'do' keyword on the same line with for/while (for ... ; do)", line})
}
}
}

return result
}

// ////////////////////////////////////////////////////////////////////////////////// //

// prefix is strings.HasPrefix wrapper
Expand Down
18 changes: 17 additions & 1 deletion check/checkers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,21 @@ func (sc *CheckSuite) TestCheckForEmptyLinesAtEnd(c *chk.C) {
c.Assert(alerts[0].Line.Index, chk.Equals, -1)
}

func (sc *CheckSuite) TestCheckBashLoops(c *chk.C) {
s, err := spec.Read("../testdata/test_10.spec")

c.Assert(err, chk.IsNil)
c.Assert(s, chk.NotNil)

alerts := checkBashLoops(s)

c.Assert(alerts, chk.HasLen, 2)
c.Assert(alerts[0].Info, chk.Equals, "Place 'do' keyword on the same line with for/while (for ... ; do)")
c.Assert(alerts[0].Line.Index, chk.Equals, 37)
c.Assert(alerts[1].Info, chk.Equals, "Place 'do' keyword on the same line with for/while (for ... ; do)")
c.Assert(alerts[1].Line.Index, chk.Equals, 49)
}

func (sc *CheckSuite) TestWithEmptyData(c *chk.C) {
s := &spec.Spec{}

Expand All @@ -346,6 +361,7 @@ func (sc *CheckSuite) TestWithEmptyData(c *chk.C) {
c.Assert(checkForIndentInFilesSection(s), chk.IsNil)
c.Assert(checkForSetupArguments(s), chk.IsNil)
c.Assert(checkForEmptyLinesAtEnd(s), chk.IsNil)
c.Assert(checkBashLoops(s), chk.IsNil)
}

func (sc *CheckSuite) TestRPMLint(c *chk.C) {
Expand Down Expand Up @@ -406,7 +422,7 @@ func (sc *CheckSuite) TestRPMLintParser(c *chk.C) {

func (sc *CheckSuite) TestAux(c *chk.C) {
// This test will fail if new checkers was added
c.Assert(getCheckers(), chk.HasLen, 18)
c.Assert(getCheckers(), chk.HasLen, 19)

r := &Report{}
c.Assert(r.IsPerfect(), chk.Equals, true)
Expand Down
43 changes: 39 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cli
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"os"

"pkg.re/essentialkaos/ek.v10/env"
Expand All @@ -17,6 +18,9 @@ import (
"pkg.re/essentialkaos/ek.v10/sliceutil"
"pkg.re/essentialkaos/ek.v10/strutil"
"pkg.re/essentialkaos/ek.v10/usage"
"pkg.re/essentialkaos/ek.v10/usage/completion/bash"
"pkg.re/essentialkaos/ek.v10/usage/completion/fish"
"pkg.re/essentialkaos/ek.v10/usage/completion/zsh"
"pkg.re/essentialkaos/ek.v10/usage/update"

"github.com/essentialkaos/perfecto/check"
Expand All @@ -28,7 +32,7 @@ import (
// App info
const (
APP = "Perfecto"
VER = "2.3.1"
VER = "2.4.0"
DESC = "Tool for checking perfectly written RPM specs"
)

Expand All @@ -42,6 +46,8 @@ const (
OPT_NO_COLOR = "nc:no-color"
OPT_HELP = "h:help"
OPT_VER = "v:version"

OPT_COMPLETION = "completion"
)

// Supported formats
Expand Down Expand Up @@ -74,6 +80,8 @@ var optMap = options.Map{
OPT_NO_COLOR: {Type: options.BOOL},
OPT_HELP: {Type: options.BOOL, Alias: "u:usage"},
OPT_VER: {Type: options.BOOL, Alias: "ver"},

OPT_COMPLETION: {},
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -90,6 +98,10 @@ func Init() {
os.Exit(1)
}

if options.Has(OPT_COMPLETION) {
genCompletion()
}

configureUI()

if options.GetB(OPT_VER) {
Expand Down Expand Up @@ -265,8 +277,13 @@ func printErrorAndExit(f string, a ...interface{}) {

// ////////////////////////////////////////////////////////////////////////////////// //

// showUsage show usage info
// showUsage prints usage info
func showUsage() {
genUsage().Render()
}

// genUsage generates usage info
func genUsage() *usage.Info {
info := usage.NewInfo("", "file…")

info.AddOption(OPT_FORMAT, "Output format {s-}(summary|tiny|short|json|xml){!}", "format")
Expand All @@ -293,10 +310,28 @@ func showUsage() {
"Check spec, generate report in JSON format and save as report.json",
)

info.Render()
return info
}

// genCompletion generates completion for different shells
func genCompletion() {
info := genUsage()

switch options.GetS(OPT_COMPLETION) {
case "bash":
fmt.Printf(bash.Generate(info, "perfecto"))
case "fish":
fmt.Printf(fish.Generate(info, "perfecto"))
case "zsh":
fmt.Printf(zsh.Generate(info, optMap, "perfecto"))
default:
os.Exit(1)
}

os.Exit(0)
}

// showAbout show info about version
// showAbout shows info about version
func showAbout() {
about := &usage.About{
App: APP,
Expand Down
34 changes: 33 additions & 1 deletion common/perfecto.spec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Summary: Tool for checking perfectly written RPM specs
Name: perfecto
Version: 2.3.1
Version: 2.4.0
Release: 0%{?dist}
Group: Development/Tools
License: EKOL
Expand Down Expand Up @@ -49,6 +49,34 @@ install -pm 755 %{name} %{buildroot}%{_bindir}/
%clean
rm -rf %{buildroot}

%post
if [[ -d %{_sysconfdir}/bash_completion.d ]] ; then
%{name} --completion=bash 1> %{_sysconfdir}/bash_completion.d/%{name} 2>/dev/null
fi

if [[ -d %{_datarootdir}/fish/vendor_completions.d ]] ; then
%{name} --completion=fish 1> %{_datarootdir}/fish/vendor_completions.d/%{name}.fish 2>/dev/null
fi

if [[ -d %{_datadir}/zsh/site-functions ]] ; then
%{name} --completion=zsh 1> %{_datadir}/zsh/site-functions/_%{name} 2>/dev/null
fi

%postun
if [[ $1 == 0 ]] ; then
if [[ -f %{_sysconfdir}/bash_completion.d/%{name} ]] ; then
rm -f %{_sysconfdir}/bash_completion.d/%{name} &>/dev/null || :
fi

if [[ -f %{_datarootdir}/fish/vendor_completions.d/%{name}.fish ]] ; then
rm -f %{_datarootdir}/fish/vendor_completions.d/%{name}.fish &>/dev/null || :
fi

if [[ -f %{_datadir}/zsh/site-functions/_%{name} ]] ; then
rm -f %{_datadir}/zsh/site-functions/_%{name} &>/dev/null || :
fi
fi

################################################################################

%files
Expand All @@ -59,6 +87,10 @@ rm -rf %{buildroot}
################################################################################

%changelog
* Tue Jul 09 2019 Anton Novojilov <[email protected]> - 2.4.0-0
- Added check for bash loops syntax
- Added completions generators

* Fri Jul 05 2019 Anton Novojilov <[email protected]> - 2.3.1-0
- Fixed bug with checking default paths without macro

Expand Down
81 changes: 81 additions & 0 deletions testdata/test_10.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
################################################################################

Summary: Test spec for perfecto
Name: perfecto-spec
Version: 1.0.0
Release: 0%{?dist}
Group: System Environment/Base
License: MIT
URL: https://domain.com

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

Source0: https://domain.com/%{name}-%{version}.tar.gz

################################################################################

%description
Test spec for perfecto app.

################################################################################

%package magic

Summary: Test subpackage for perfecto
Group: System Environment/Base

%description magic
Test subpackge for perfecto app.

################################################################################

%prep
%setup -qn %{name}-%{version}

%build

while true
do
rm -f $file
done

%{__make} %{?_smp_mflags}

%install
rm -rf %{buildroot}

%{make_install} PREFIX=%{buildroot}%{_prefix}

for file in $(ls -1)
do
rm -f $file
done

%clean
# perfecto:absolve 2
rm -rf %{buildroot}

%post
%{__chkconfig} --add %{name} &>/dev/null || :

%preun
%{__chkconfig} --del %{name} &> /dev/null || :

%postun
%{__chkconfig} --del %{name} &> /dev/null || :

################################################################################

%files
%defattr(-,root,root,-)
%{_bindir}/%{name}

%files magic
%defattr(-,root,root,-)
%{_bindir}/%{name}-magic

################################################################################

%changelog
* Wed Jan 24 2018 Anton Novojilov <[email protected]> - 1.0.0-0
- Test changelog record

0 comments on commit 674100f

Please sign in to comment.