github.com/tiagovtristao/plz@v13.4.0+incompatible/src/query/print_test.go (about)

     1  package query
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/thought-machine/please/src/core"
    12  )
    13  
    14  func TestAllFieldsArePresentAndAccountedFor(t *testing.T) {
    15  	target := core.BuildTarget{}
    16  	var buf bytes.Buffer
    17  	p := newPrinter(&buf, &target, 0)
    18  	p.PrintTarget()
    19  	assert.False(t, p.error, "Appears we do not know how to print some fields")
    20  }
    21  
    22  func TestPrintOutput(t *testing.T) {
    23  	target := core.NewBuildTarget(core.ParseBuildLabel("//src/query:test_print_output", ""))
    24  	target.AddSource(src("file.go"))
    25  	target.AddSource(src(":target1"))
    26  	target.AddSource(src("//src/query:target2"))
    27  	target.AddSource(src("//src/query:target3|go"))
    28  	target.AddSource(src("//src/core:core"))
    29  	target.AddOutput("out1.go")
    30  	target.AddOutput("out2.go")
    31  	target.Command = "cp $SRCS $OUTS"
    32  	target.Tools = append(target.Tools, src("//tools:tool1"))
    33  	target.IsBinary = true
    34  	s := testPrint(target)
    35  	expected := `  build_rule(
    36        name = 'test_print_output',
    37        srcs = [
    38            'file.go',
    39            '//src/query:target1',
    40            '//src/query:target2',
    41            '//src/query:target3|go',
    42            '//src/core:core',
    43        ],
    44        outs = [
    45            'out1.go',
    46            'out2.go',
    47        ],
    48        cmd = 'cp $SRCS $OUTS',
    49        binary = True,
    50        tools = ['//tools:tool1'],
    51    )
    52  
    53  `
    54  	assert.Equal(t, expected, s)
    55  }
    56  
    57  func TestFilegroupOutput(t *testing.T) {
    58  	target := core.NewBuildTarget(core.ParseBuildLabel("//src/query:test_filegroup_output", ""))
    59  	target.AddSource(src("file.go"))
    60  	target.AddSource(src(":target1"))
    61  	target.IsFilegroup = true
    62  	target.Visibility = core.WholeGraph
    63  	s := testPrint(target)
    64  	expected := `  filegroup(
    65        name = 'test_filegroup_output',
    66        srcs = [
    67            'file.go',
    68            '//src/query:target1',
    69        ],
    70        visibility = ['PUBLIC'],
    71    )
    72  
    73  `
    74  	assert.Equal(t, expected, s)
    75  }
    76  
    77  func TestTestOutput(t *testing.T) {
    78  	target := core.NewBuildTarget(core.ParseBuildLabel("//src/query:test_test_output", ""))
    79  	target.AddSource(src("file.go"))
    80  	target.IsTest = true
    81  	target.IsBinary = true
    82  	target.BuildTimeout = 30 * time.Second
    83  	target.TestTimeout = 60 * time.Second
    84  	target.Flakiness = 2
    85  	s := testPrint(target)
    86  	expected := `  build_rule(
    87        name = 'test_test_output',
    88        srcs = ['file.go'],
    89        binary = True,
    90        test = True,
    91        flaky = 2,
    92        timeout = 30,
    93        test_timeout = 60,
    94    )
    95  
    96  `
    97  	assert.Equal(t, expected, s)
    98  }
    99  
   100  type postBuildFunction struct{}
   101  
   102  func (f postBuildFunction) Call(target *core.BuildTarget, output string) error { return nil }
   103  func (f postBuildFunction) String() string                                     { return "<func ref>" }
   104  
   105  func TestPostBuildOutput(t *testing.T) {
   106  	target := core.NewBuildTarget(core.ParseBuildLabel("//src/query:test_post_build_output", ""))
   107  	target.PostBuildFunction = postBuildFunction{}
   108  	target.AddCommand("opt", "/bin/true")
   109  	target.AddCommand("dbg", "/bin/false")
   110  	s := testPrint(target)
   111  	expected := `  build_rule(
   112        name = 'test_post_build_output',
   113        cmd = {
   114            'dbg': '/bin/false',
   115            'opt': '/bin/true',
   116        },
   117        post_build = '<func ref>',
   118    )
   119  
   120  `
   121  	assert.Equal(t, expected, s)
   122  }
   123  
   124  func TestContainerOutput(t *testing.T) {
   125  	target := core.NewBuildTarget(core.ParseBuildLabel("//src/query:test_container_output", ""))
   126  	target.SetContainerSetting("dockerimage", "alpine:3.5")
   127  	target.SetContainerSetting("dockeruser", "test")
   128  	s := testPrint(target)
   129  	expected := `  build_rule(
   130        name = 'test_container_output',
   131        container = {
   132            'docker_image': 'alpine:3.5',
   133            'docker_user': 'test',
   134        },
   135    )
   136  
   137  `
   138  	assert.Equal(t, expected, s)
   139  }
   140  
   141  func TestPrintFields(t *testing.T) {
   142  	target := core.NewBuildTarget(core.ParseBuildLabel("//src/query:test_print_fields", ""))
   143  	target.AddLabel("go")
   144  	target.AddLabel("test")
   145  	s := testPrintFields(target, []string{"labels"})
   146  	assert.Equal(t, "go\ntest\n", s)
   147  }
   148  
   149  func testPrint(target *core.BuildTarget) string {
   150  	var buf bytes.Buffer
   151  	newPrinter(&buf, target, 2).PrintTarget()
   152  	return buf.String()
   153  }
   154  
   155  func testPrintFields(target *core.BuildTarget, fields []string) string {
   156  	var buf bytes.Buffer
   157  	newPrinter(&buf, target, 0).PrintFields(fields)
   158  	return buf.String()
   159  }
   160  
   161  func src(in string) core.BuildInput {
   162  	pkg := core.NewPackage("src/query")
   163  	if strings.HasPrefix(in, "//") || strings.HasPrefix(in, ":") {
   164  		return core.MustParseNamedOutputLabel(in, pkg)
   165  	}
   166  	return core.FileLabel{File: in, Package: pkg.Name}
   167  }