github.com/mssola/todo@v0.0.0-20181029153210-d25348dc3f48/script/climate (about)

     1  #!/usr/bin/perl -w
     2  # Copyright (C) 2014-2017 Miquel Sabaté Solà
     3  #
     4  # This Source Code Form is subject to the terms of the Mozilla Public
     5  # License, v. 2.0. If a copy of the MPL was not distributed with this
     6  # file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7  
     8  # This script allows us to perform a check of the state of a Go package. In
     9  # order to do this, it checks the following options:
    10  #
    11  #   1. The code coverage: it uses the `cover` tool to determine the code
    12  #   coverage of the given package. A package is considered to have a good code
    13  #   coverage when it surpasses the value of the "$threshold" configurable
    14  #   value.
    15  #   2. The code style: it uses the `gofmt` tool to determine if it is following
    16  #   the proper code style. You can check the parameters passed to the `gofmt`
    17  #   tool in the "$fmtparams" configurable value.
    18  #
    19  # You can call this script with the following optional arguments:
    20  #
    21  #   -a
    22  #       It will tell this script to keep going, even if the `cover` tool says
    23  #       that the coverage for the given package is below our threshold.
    24  #
    25  #   -o
    26  #       Tell this script to not open a new tab in the web browser with the
    27  #       results as given by the `cover` tool.
    28  #   -t
    29  #       Tell this script to take a threshold different than the default one.
    30  #
    31  # Therefore, this command has the following usage:
    32  #
    33  #   $ climate [-a] [-o] [-t number] [-h | --help] package-name
    34  #
    35  
    36  use strict;
    37  use File::Basename;
    38  use Cwd 'abs_path', 'getcwd';
    39  
    40  
    41  ##
    42  # Config values. Ideally you should only modify these values to adapt this
    43  # script to your project.
    44  #
    45  # NOTE: path values should *not* start and/or end with a slash.
    46  
    47  
    48  # The base path that this script will use. By default it picks the current
    49  # working directory.
    50  my $base = abs_path(getcwd);
    51  
    52  # The threshold is the percentage in which we consider that the given package
    53  # is covered enough by tests. It defaults to 90.0%
    54  my $threshold = 90.0;
    55  
    56  
    57  ##
    58  # And here starts the program itself.
    59  
    60  
    61  # Show the usage message.
    62  sub usage {
    63      print "Usage: climate [-a] [-o] [-h | --help] package-name\n";
    64      print "  -a  Continue even if the cover test is below our threshold.\n";
    65      print "  -o  Don't open a new tab with the coverage results.\n";
    66      print "  -t  Pass a different threshold than the default one.\n";
    67      print "  -h  Show this message.\n";
    68  }
    69  
    70  # Initialization.
    71  my $pkg = $ARGV[-1];
    72  my $all = 0;
    73  
    74  # Parsing options.
    75  if (@ARGV == 0) {
    76      usage();
    77      exit(1);
    78  }
    79  
    80  my %opts = ('a', 0, 'o', 1, 't', $threshold);
    81  for (my $it = 0; $it < @ARGV; $it++) {
    82      if ($ARGV[$it] eq '-a') {
    83          $opts{'a'} = 1;
    84      } elsif ($ARGV[$it] eq '-o') {
    85          $opts{'o'} = 0;
    86      } elsif ($ARGV[$it] eq '-t') {
    87          usage() if (!$ARGV[$it + 1]);
    88          $opts{'t'} = sprintf '%.1f', $ARGV[$it + 1];
    89          $it++;
    90      } elsif ($ARGV[$it] eq '-h' || $ARGV[$it] eq '--help') {
    91          usage();
    92          exit(0);
    93      } else {
    94          if ($it == @ARGV - 1) {
    95              last;
    96          }
    97          print "Unknown option `$ARGV[$it]'.\n";
    98          usage();
    99          exit(1);
   100      }
   101  }
   102  
   103  # Cover tool.
   104  chdir("$base/$pkg") or die $!;
   105  my $cover = `go tool cover 2>&1`;
   106  
   107  # First check whether we have to get the cover tool or not.
   108  if ($cover =~ /^go tool: no such tool "cover"/) {
   109      `go get golang.org/x/tools/cmd/cover`;
   110  }
   111  
   112  # Execute the cover tool.
   113  $cover = `go test -coverprofile=c.out -covermode=count`;
   114  my $error = 0;
   115  if ($cover =~ /coverage:\s?(.+)%/) {
   116      my $fl = sprintf '%.1f', $1;
   117      if ($fl < $opts{'t'}) {
   118          print "Coverage required: $opts{'t'}%; got: $fl%\n";
   119          if (!$opts{'a'}) {
   120              `rm -f c.out`;
   121              exit(1);
   122          }
   123          $error = 1;
   124      } else {
   125          print "The tests are covering the $fl% of this package!\n";
   126      }
   127      if ($opts{'o'}) {
   128          `go tool cover -html=c.out`;
   129      }
   130      `rm -f c.out`;
   131      if ($error) {
   132          exit(1);
   133      }
   134  } else {
   135      print "No tests found in package '$pkg'.\n";
   136  }