github.com/DEVELOPEST/gtm-core@v1.0.3/command/clean_test.go (about)

     1  // Copyright 2016 Michael Schenk. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package command
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/DEVELOPEST/gtm-core/project"
    14  	"github.com/DEVELOPEST/gtm-core/util"
    15  	"github.com/mitchellh/cli"
    16  )
    17  
    18  func TestCleanYes(t *testing.T) {
    19  	repo := util.NewTestRepo(t, false)
    20  	defer repo.Remove()
    21  	repo.Seed()
    22  	os.Chdir(repo.Workdir())
    23  
    24  	(InitCmd{UI: new(cli.MockUi)}).Run([]string{})
    25  
    26  	ui := new(cli.MockUi)
    27  	c := CleanCmd{UI: ui}
    28  
    29  	args := []string{"-yes"}
    30  	rc := c.Run(args)
    31  
    32  	if rc != 0 {
    33  		t.Errorf("gtm clean(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
    34  	}
    35  }
    36  
    37  func TestTerminalOnly(t *testing.T) {
    38  	repo := util.NewTestRepo(t, false)
    39  	defer repo.Remove()
    40  	repo.Seed()
    41  	os.Chdir(repo.Workdir())
    42  
    43  	(InitCmd{UI: new(cli.MockUi)}).Run([]string{})
    44  
    45  	ui := new(cli.MockUi)
    46  	c := CleanCmd{UI: ui}
    47  
    48  	args := []string{"-terminal-only", "-yes"}
    49  	rc := c.Run(args)
    50  
    51  	if rc != 0 {
    52  		t.Errorf("gtm clean(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
    53  	}
    54  }
    55  
    56  func TestAppOnly(t *testing.T) {
    57  	repo := util.NewTestRepo(t, false)
    58  	defer repo.Remove()
    59  	repo.Seed()
    60  	os.Chdir(repo.Workdir())
    61  
    62  	repo.SaveFile("browser.app", project.GTMDir, "")
    63  	repo.SaveFile("1458496803.event", project.GTMDir, filepath.Join("event", "event.go"))
    64  	repo.SaveFile("1458497804.event", project.GTMDir, filepath.Join(project.GTMDir, "browser.app"))
    65  
    66  	(InitCmd{UI: new(cli.MockUi)}).Run([]string{})
    67  
    68  	ui := new(cli.MockUi)
    69  	c := CleanCmd{UI: ui}
    70  
    71  	args := []string{"-app-only", "-yes"}
    72  	rc := c.Run(args)
    73  
    74  	if rc != 0 {
    75  		t.Errorf("gtm clean(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
    76  	}
    77  
    78  	if !repo.FileExists("1458496803.event", project.GTMDir) {
    79  		t.Errorf("gtm clean(%+v), want non-app event to not be deleted, but was deleted", args)
    80  	}
    81  
    82  	if repo.FileExists("1458497804.event", project.GTMDir) {
    83  		t.Errorf("gtm clean(%+v), want app event to be deleted, but was found", args)
    84  	}
    85  }
    86  
    87  func TestCleanInvalidOption(t *testing.T) {
    88  	ui := new(cli.MockUi)
    89  	c := CleanCmd{UI: ui}
    90  
    91  	args := []string{"-invalid"}
    92  	rc := c.Run(args)
    93  
    94  	if rc != 1 {
    95  		t.Errorf("gtm clean(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter)
    96  	}
    97  	if !strings.Contains(ui.OutputWriter.String(), "Usage:") {
    98  		t.Errorf("gtm clean(%+v), want 'Usage:'  got %d, %s", args, rc, ui.OutputWriter.String())
    99  	}
   100  }