github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/cmd/jiri/run_hooks_test.go (about)

     1  // Copyright 2017 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"path/filepath"
    11  	"strconv"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/btwiuse/jiri/jiritest"
    16  	"github.com/btwiuse/jiri/log"
    17  	"github.com/btwiuse/jiri/project"
    18  )
    19  
    20  func setDefaultRunHookFlags() {
    21  	runHooksFlags.localManifest = false
    22  }
    23  func createRunHookProjects(t *testing.T, fake *jiritest.FakeJiriRoot, numProjects int) []project.Project {
    24  	localProjects := []project.Project{}
    25  	for i := 0; i < numProjects; i++ {
    26  		name := fmt.Sprintf("project-%d", i)
    27  		path := fmt.Sprintf("path-%d", i)
    28  		if err := fake.CreateRemoteProject(name); err != nil {
    29  			t.Fatal(err)
    30  		}
    31  		p := project.Project{
    32  			Name:   name,
    33  			Path:   filepath.Join(fake.X.Root, path),
    34  			Remote: fake.Projects[name],
    35  		}
    36  		localProjects = append(localProjects, p)
    37  		if err := fake.AddProject(p); err != nil {
    38  			t.Fatal(err)
    39  		}
    40  	}
    41  	for i, localProject := range localProjects {
    42  		writeFile(t, fake.X, fake.Projects[localProject.Name], "file1"+strconv.Itoa(i), "file1"+strconv.Itoa(i))
    43  	}
    44  	return localProjects
    45  }
    46  
    47  func TestRunHookSimple(t *testing.T) {
    48  	setDefaultRunHookFlags()
    49  	fake, cleanup := jiritest.NewFakeJiriRoot(t)
    50  	defer cleanup()
    51  	projects := createRunHookProjects(t, fake, 1)
    52  	err := fake.AddHook(project.Hook{Name: "hook1",
    53  		Action:      "action.sh",
    54  		ProjectName: projects[0].Name})
    55  
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if err = fake.UpdateUniverse(false); err == nil {
    60  		t.Fatal("project update should throw error as there is no action.sh script")
    61  	}
    62  
    63  	if err := runHooks(fake.X, nil); err == nil {
    64  		t.Fatal("runhooks should throw error as there is no action.sh script")
    65  	}
    66  }
    67  
    68  func TestRunHookLocalManifest(t *testing.T) {
    69  	setDefaultRunHookFlags()
    70  	fake, cleanup := jiritest.NewFakeJiriRoot(t)
    71  	defer cleanup()
    72  	projects := createRunHookProjects(t, fake, 1)
    73  	err := fake.AddHook(project.Hook{Name: "hook1",
    74  		Action:      "action.sh",
    75  		ProjectName: projects[0].Name})
    76  
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if err = fake.UpdateUniverse(false); err == nil {
    81  		t.Fatal("project update should throw error as there is no action.sh script")
    82  	}
    83  
    84  	manifest, err := fake.ReadRemoteManifest()
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	manifest.Hooks[0].Action = "action1.sh"
    89  	manifest.ToFile(fake.X, filepath.Join(fake.X.Root, jiritest.ManifestProjectPath, jiritest.ManifestFileName))
    90  	runHooksFlags.localManifest = true
    91  	buf := bytes.NewBufferString("")
    92  	fake.X.Logger = log.NewLogger(fake.X.Logger.LoggerLevel, fake.X.Color, false, 0, 100, nil, buf)
    93  	if err := runHooks(fake.X, nil); err == nil {
    94  		t.Fatal("runhooks should throw error as there is no action.sh script")
    95  	} else if !strings.Contains(buf.String(), "action1.sh") {
    96  		t.Fatalf("runhooks should throw error for action1.sh script, the error it threw: %s", buf.String())
    97  	}
    98  }