當前位置: 首頁>>代碼示例>>Golang>>正文


Golang capture.All函數代碼示例

本文整理匯總了Golang中github.com/mssola/capture.All函數的典型用法代碼示例。如果您正苦於以下問題:Golang All函數的具體用法?Golang All怎麽用?Golang All使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了All函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestCheckImageExists

func TestCheckImageExists(t *testing.T) {
	var found bool
	var err error

	dockerClient = &mockClient{waitSleep: 100 * time.Millisecond}

	expected := []string{"latest", "13.2"}
	for _, e := range expected {
		capture.All(func() {
			found, err = checkImageExists("opensuse", e)
		})

		if err != nil {
			t.Fatal("Unexpected error")
		}
		if found != true {
			t.Fatal("The image should have been found")
		}
	}

	not_expected := []string{"unexpected_tag"}
	for _, unexpected := range not_expected {
		capture.All(func() {
			found, err = checkImageExists("opensuse", unexpected)
		})

		if err != nil {
			t.Fatal("Unexpected error")
		}
		if found != false {
			t.Fatal("The image should not have been found")
		}
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:34,代碼來源:images_test.go

示例2: TestSetupLoggerHome

func TestSetupLoggerHome(t *testing.T) {
	abs, err := filepath.Abs("test")
	if err != nil {
		t.Fatalf("Could not setup the test suite: %v\n", err)
	}
	home := os.Getenv("HOME")
	defer func() {
		_ = os.Setenv("HOME", home)
		_ = os.Remove(filepath.Join(abs, logFileName))
	}()
	_ = os.Setenv("HOME", abs)

	res := capture.All(func() {
		setupLogger(testContext([]string{}, false))
		log.Printf("Test")
	})
	if len(res.Stdout) != 0 {
		t.Fatal("Nothing should've been printed to stdout\n")
	}
	contents, err := ioutil.ReadFile(filepath.Join(abs, logFileName))
	if err != nil {
		t.Fatalf("Could not read contents of the log: %v\n", err)
	}
	if !strings.HasSuffix(string(contents), "Test\n") {
		t.Fatalf("'%v' expected to have logged 'Test'\n", string(contents))
	}
}
開發者ID:cyphar,項目名稱:zypper-docker,代碼行數:27,代碼來源:logger_test.go

示例3: TestImagesListOk

func TestImagesListOk(t *testing.T) {
	dockerClient = &mockClient{waitSleep: 100 * time.Millisecond}
	setupTestExitStatus()

	buffer := bytes.NewBuffer([]byte{})
	log.SetOutput(buffer)

	res := capture.All(func() { imagesCmd(testContext([]string{}, false)) })

	lines := strings.Split(string(res.Stdout), "\n")
	if len(lines) != 7 {
		t.Fatal("Wrong number of lines")
	}
	if !strings.HasPrefix(lines[1], "REPOSITORY") {
		t.Fatal("Wrong contents")
	}
	str := "opensuse            latest              1                   Less than a second ago   254.5 MB"
	if lines[2] != str {
		t.Fatal("Wrong contents")
	}
	str = "opensuse            tag                 1                   Less than a second ago   254.5 MB"
	if lines[3] != str {
		t.Fatal("Wrong contents")
	}
	str = "opensuse            13.2                2                   Less than a second ago   254.5 MB"
	if lines[4] != str {
		t.Fatal("Wrong contents")
	}
	if exitInvocations != 1 && lastCode != 0 {
		t.Fatal("Wrong exit code")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:32,代碼來源:images_test.go

示例4: TestPsCommandMatches

func TestPsCommandMatches(t *testing.T) {
	cacheFile := getCacheFile()
	cacheFile.Outdated = []string{"2"} // this is the Id of the opensuse:13.2 image
	cacheFile.Other = []string{"3"}    // this is the Id of the ubuntu:latest image
	cacheFile.flush()

	setupTestExitStatus()
	safeClient.client = &mockClient{}

	buffer := bytes.NewBuffer([]byte{})
	log.SetOutput(buffer)
	rec := capture.All(func() { psCmd(testContext([]string{}, false)) })

	if !strings.Contains(buffer.String(), "Cannot analyze container 4 [foo]") {
		t.Fatal("Wrong message")
	}
	if !strings.Contains(string(rec.Stdout), "Running containers whose images have been updated") {
		t.Fatal("Wrong message")
	}
	if !strings.Contains(string(rec.Stdout), "The following containers have an unknown state") &&
		!strings.Contains(string(rec.Stdout), "busybox") &&
		!strings.Contains(string(rec.Stdout), "foo") {
		t.Fatal("Wrong message")
	}
	if !strings.Contains(string(rec.Stdout), "The following containers have been ignored") &&
		!strings.Contains(string(rec.Stdout), "ubuntu") {
		t.Fatal("Wrong message")
	}
	if exitInvocations != 0 {
		t.Fatalf("Should not have exited with an error")
	}
	if lastCode != 0 {
		t.Fatalf("Exit status should be 1, %v given", lastCode)
	}
}
開發者ID:cyphar,項目名稱:zypper-docker,代碼行數:35,代碼來源:ps_test.go

示例5: TestPsCommandNoMatches

func TestPsCommandNoMatches(t *testing.T) {
	cache := os.Getenv("XDG_CACHE_HOME")
	abs, _ := filepath.Abs(".")
	test := filepath.Join(abs, "test")

	defer func() {
		_ = os.Setenv("XDG_CACHE_HOME", cache)
	}()

	_ = os.Setenv("XDG_CACHE_HOME", test)

	setupTestExitStatus()
	dockerClient = &mockClient{}

	buffer := bytes.NewBuffer([]byte{})
	log.SetOutput(buffer)
	capture.All(func() { psCmd(testContext([]string{}, false)) })

	if strings.Contains(buffer.String(), "Running containers whose images have been updated") {
		t.Fatal("It should not have found matches")
	}
	if exitInvocations != 0 {
		t.Fatalf("Should not have exited with an error")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:25,代碼來源:ps_test.go

示例6: TestImagesListUsingCache

func TestImagesListUsingCache(t *testing.T) {
	safeClient.client = &mockClient{waitSleep: 100 * time.Millisecond}
	setupTestExitStatus()

	// Dump some dummy value.
	cd := getCacheFile()
	cd.Suse = []string{"1"}
	cd.Other = []string{"3"}
	cd.flush()

	buffer := bytes.NewBuffer([]byte{})
	log.SetOutput(buffer)

	res := capture.All(func() { imagesCmd(testContext([]string{}, false)) })

	testReaderData(t, bytes.NewBuffer(res.Stdout), []string{
		"REPOSITORY",
		"opensuse            latest              1",
		"opensuse            tag                 1",
		"opensuse            13.2                2",
		"busybox             latest              5",
	})

	if exitInvocations != 1 && lastCode != 0 {
		t.Fatal("Wrong exit code")
	}
}
開發者ID:mssola,項目名稱:zypper-docker,代碼行數:27,代碼來源:images_test.go

示例7: TestHostConfig

func TestHostConfig(t *testing.T) {
	hc := getHostConfig()
	if len(hc.ExtraHosts) != 0 {
		t.Fatalf("Wrong number of extra hosts: %v; Expected: 1", len(hc.ExtraHosts))
	}

	originalArgs := os.Args
	defer func() {
		os.Args = originalArgs
		currentContext = nil
	}()
	os.Args = []string{"exe", "--add-host", "host:ip", "test"}

	app := newApp()
	app.Commands = []cli.Command{{Name: "test", Action: getCmd("test", func(*cli.Context) {})}}
	capture.All(func() { app.RunAndExitOnError() })

	hc = getHostConfig()
	if len(hc.ExtraHosts) != 1 {
		t.Fatalf("Wrong number of extra hosts: %v; Expected: 1", len(hc.ExtraHosts))
	}
	if hc.ExtraHosts[0] != "host:ip" {
		t.Fatalf("Did not expect %v", hc.ExtraHosts[0])
	}
}
開發者ID:cyphar,項目名稱:zypper-docker,代碼行數:25,代碼來源:client_test.go

示例8: TestLogAndPrintfAndFatalf

func TestLogAndPrintfAndFatalf(t *testing.T) {
	setupTestExitStatus()
	dm := debugMode

	// Debug mode: false

	debugMode = false

	buffer := bytes.NewBuffer([]byte{})
	log.SetOutput(buffer)

	res := capture.All(func() { logAndFatalf("Here") })

	if !strings.Contains(buffer.String(), "Here") {
		t.Fatalf("Wrong logged value!")
	}
	if !strings.Contains(string(res.Stdout), "Here") {
		t.Fatalf("Wrong logged value!")
	}
	if lastCode != 1 {
		t.Fatalf("It should've failed!")
	}

	// Debug mode: true

	debugMode = true
	lastCode = 0

	buffer = bytes.NewBuffer([]byte{})
	log.SetOutput(buffer)

	res = capture.All(func() { logAndPrintf("Here") })

	if !strings.Contains(buffer.String(), "Here") {
		t.Fatalf("Wrong logged value!")
	}
	if len(res.Stdout) != 0 {
		t.Fatalf("Should've been empty!")
	}
	if lastCode != 0 {
		t.Fatalf("lastCode should've not been changed")
	}

	debugMode = dm
}
開發者ID:cyphar,項目名稱:zypper-docker,代碼行數:45,代碼來源:logger_test.go

示例9: TestUpdateCommandRunAndCommitFailure

func TestUpdateCommandRunAndCommitFailure(t *testing.T) {
	setupTestExitStatus()
	dockerClient = &mockClient{startFail: true}

	capture.All(func() { updateCmd(testContext([]string{"ori", "new:1.0.0"}, false)) })

	if exitInvocations != 1 {
		t.Fatalf("Expected to have exited with error")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:10,代碼來源:updates_test.go

示例10: TestUpdateCommandInvalidTargetName

func TestUpdateCommandInvalidTargetName(t *testing.T) {
	setupTestExitStatus()
	dockerClient = &mockClient{}

	capture.All(func() { updateCmd(testContext([]string{"ori", "WRONG"}, false)) })

	if exitInvocations != 1 {
		t.Fatalf("Expected to have exited with error")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:10,代碼來源:updates_test.go

示例11: TestPatchCommandInvalidTargetName

func TestPatchCommandInvalidTargetName(t *testing.T) {
	setupTestExitStatus()
	dockerClient = &mockClient{}

	capture.All(func() { patchCmd(testPatchContext("ori", "WRONG")) })

	if exitInvocations != 1 {
		t.Fatalf("Expected to have exited with error")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:10,代碼來源:patches_test.go

示例12: TestPatchCommandImageOverwriteDetected

func TestPatchCommandImageOverwriteDetected(t *testing.T) {
	setupTestExitStatus()
	dockerClient = &mockClient{listFail: true}

	capture.All(func() { patchCmd(testPatchContext("ori", "new:1.0.0")) })

	if exitInvocations != 1 {
		t.Fatalf("Expected to have exited with error")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:10,代碼來源:patches_test.go

示例13: run

// run the test cases.
func (cases testCases) run(t *testing.T, cmd func(*cli.Context), command, debug string) {
	for _, test := range cases {
		// Skip if this not the one being debugged.
		if debug != "" && debug != test.desc {
			continue
		}

		setupTestExitStatus()
		safeClient.client = test.client

		buffer := bytes.NewBuffer([]byte{})
		log.SetOutput(buffer)
		captured := capture.All(func() { cmd(testContext(test.args, false)) })

		// Exit status code
		if lastCode != test.code {
			t.Fatalf("[%s] Expected to have exited with code %v, %v was received.",
				test.desc, test.code, lastCode)
		}

		// Log
		if test.msg == "" {
			lines := strings.Split(buffer.String(), "\n")
			// The first line might be the cache failing to be loaded.
			if !(len(lines) == 1 || len(lines) == 2) || lines[len(lines)-1] != "" {
				t.Fatalf("Should've loggeed nothing, logged:\n%s\n", buffer.String())
			}
		} else {
			if !strings.Contains(buffer.String(), test.msg) {
				t.Fatalf("[%s] Wrong logged message.\nExpecting:\n%s\n===\nReceived:\n%s\n",
					test.desc, test.msg, buffer.String())
			}
		}

		// Stdout
		if test.logAndStdout {
			test.stdout = test.msg
		}
		if test.stdout != "" {
			if !strings.Contains(string(captured.Stdout), test.stdout) {
				t.Fatalf("[%s] Wrong stdout.\nExpecting:\n%s\n===\nReceived:\n%s\n",
					test.desc, test.stdout, string(captured.Stdout))
			}
		}
		if lastCode == 0 && command != "" {
			if command != testCommand() {
				t.Fatalf("[%s] Wrong command. Expecting '%s', '%s' received.\n",
					test.desc, command, testCommand())
			}
		}
	}
}
開發者ID:cyphar,項目名稱:zypper-docker,代碼行數:53,代碼來源:utils_test.go

示例14: TestCheckImageListFail

func TestCheckImageListFail(t *testing.T) {
	dockerClient = &mockClient{listFail: true}

	var err error

	capture.All(func() {
		_, err = checkImageExists("opensuse", "bar")
	})

	if err == nil {
		t.Fatal("Error did not occur")
	}
}
開發者ID:jloehel,項目名稱:zypper-docker,代碼行數:13,代碼來源:images_test.go

示例15: TestSetupLoggerDebug

func TestSetupLoggerDebug(t *testing.T) {
	// Set debug mode.
	set := flag.NewFlagSet("test", 0)
	set.Bool("debug", true, "doc")
	c := cli.NewContext(nil, set, nil)

	res := capture.All(func() {
		setupLogger(c)
		log.Printf("Test")
	})
	if !strings.HasSuffix(string(res.Stdout), "Test\n") {
		t.Fatalf("'%v' expected to have logged 'Test'\n", string(res.Stdout))
	}
}
開發者ID:cyphar,項目名稱:zypper-docker,代碼行數:14,代碼來源:logger_test.go


注:本文中的github.com/mssola/capture.All函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。