<commit_before>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main_test

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"internal/testenv"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strings"
	"testing"
)

const (
	// Data directory, also the package directory for the test.
	testdata = "testdata"

	// Binaries we compile.
	testcover = "./testcover.exe"
)

var (
	// Files we use.
	testMain     = filepath.Join(testdata, "main.go")
	testTest     = filepath.Join(testdata, "test.go")
	coverInput   = filepath.Join(testdata, "test_line.go")
	coverOutput  = filepath.Join(testdata, "test_cover.go")
	coverProfile = filepath.Join(testdata, "profile.cov")

	// The HTML test files are in a separate directory
	// so they are a complete package.
	htmlProfile = filepath.Join(testdata, "html", "html.cov")
	htmlHTML    = filepath.Join(testdata, "html", "html.html")
	htmlGolden  = filepath.Join(testdata, "html", "html.golden")
)

var debug = flag.Bool("debug", false, "keep rewritten files for debugging")

// Run this shell script, but do it in Go so it can be run by "go test".
//
//	replace the word LINE with the line number < testdata/test.go > testdata/test_line.go
// 	go build -o ./testcover
// 	./testcover -mode=count -var=CoverTest -o ./testdata/test_cover.go testdata/test_line.go
//	go run ./testdata/main.go ./testdata/test.go
//
func TestCover(t *testing.T) {
	testenv.MustHaveGoBuild(t)

	// Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
	file, err := ioutil.ReadFile(testTest)
	if err != nil {
		t.Fatal(err)
	}
	lines := bytes.Split(file, []byte("\n"))
	for i, line := range lines {
		lines[i] = bytes.Replace(line, []byte("LINE"), []byte(fmt.Sprint(i+1)), -1)
	}

	// Add a function that is not gofmt'ed. This used to cause a crash.
	// We don't put it in test.go because then we would have to gofmt it.
	// Issue 23927.
	lines = append(lines, []byte("func unFormatted() {"),
		[]byte("\tif true {"),
		[]byte("\t}else{"),
		[]byte("\t}"),
		[]byte("}"))
	lines = append(lines, []byte("func unFormatted2(b bool) {if b{}else{}}"))

	if err := ioutil.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666); err != nil {
		t.Fatal(err)
	}

	// defer removal of test_line.go
	if !*debug {
		defer os.Remove(coverInput)
	}

	// go build -o testcover
	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", testcover)
	run(cmd, t)

	// defer removal of testcover
	defer os.Remove(testcover)

	// ./testcover -mode=count -var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest -o ./testdata/test_cover.go testdata/test_line.go
	cmd = exec.Command(testcover, "-mode=count", "-var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest", "-o", coverOutput, coverInput)
	run(cmd, t)

	// defer removal of ./testdata/test_cover.go
	if !*debug {
		defer os.Remove(coverOutput)
	}

	// go run ./testdata/main.go ./testdata/test.go
	cmd = exec.Command(testenv.GoToolPath(t), "run", testMain, coverOutput)
	run(cmd, t)

	file, err = ioutil.ReadFile(coverOutput)
	if err != nil {
		t.Fatal(err)
	}
	// compiler directive must appear right next to function declaration.
	if got, err := regexp.MatchString(".*\n//go:nosplit\nfunc someFunction().*", string(file)); err != nil || !got {
		t.Error("misplaced compiler directive")
	}
	// "go:linkname" compiler directive should be present.
	if got, err := regexp.MatchString(`.*go\:linkname some\_name some\_name.*`, string(file)); err != nil || !got {
		t.Error("'go:linkname' compiler directive not found")
	}

	// Other comments should be preserved too.
	c := ".*// This comment didn't appear in generated go code.*"
	if got, err := regexp.MatchString(c, string(file)); err != nil || !got {
		t.Errorf("non compiler directive comment %q not found", c)
	}
}

// TestDirectives checks that compiler directives are preserved and positioned
// correctly. Directives that occur before top-level declarations should remain
// above those declarations, even if they are not part of the block of
// documentation comments.
func TestDirectives(t *testing.T) {
	// Read the source file and find all the directives. We'll keep
	// track of whether each one has been seen in the output.
	testDirectives := filepath.Join(testdata, "directives.go")
	source, err := ioutil.ReadFile(testDirectives)
	if err != nil {
		t.Fatal(err)
	}
	sourceDirectives := findDirectives(source)

	// go tool cover -mode=atomic ./testdata/directives.go
	cmd := exec.Command(testenv.GoToolPath(t), "tool", "cover", "-mode=atomic", testDirectives)
	cmd.Stderr = os.Stderr
	output, err := cmd.Output()
	if err != nil {
		t.Fatal(err)
	}

	// Check that all directives are present in the output.
	outputDirectives := findDirectives(output)
	foundDirective := make(map[string]bool)
	for _, p := range sourceDirectives {
		foundDirective[p.name] = false
	}
	for _, p := range outputDirectives {
		if found, ok := foundDirective[p.name]; !ok {
			t.Errorf("unexpected directive in output: %s", p.text)
		} else if found {
			t.Errorf("directive found multiple times in output: %s", p.text)
		}
		foundDirective[p.name] = true
	}
	for name, found := range foundDirective {
		if !found {
			t.Errorf("missing directive: %s", name)
		}
	}

	// Check that directives that start with the name of top-level declarations
	// come before the beginning of the named declaration and after the end
	// of the previous declaration.
	fset := token.NewFileSet()
	astFile, err := parser.ParseFile(fset, testDirectives, output, 0)
	if err != nil {
		t.Fatal(err)
	}

	prevEnd := 0
	for _, decl := range astFile.Decls {
		var name string
		switch d := decl.(type) {
		case *ast.FuncDecl:
			name = d.Name.Name
		case *ast.GenDecl:
			if len(d.Specs) == 0 {
				// An empty group declaration. We still want to check that
				// directives can be associated with it, so we make up a name
				// to match directives in the test data.
				name = "_empty"
			} else if spec, ok := d.Specs[0].(*ast.TypeSpec); ok {
				name = spec.Name.Name
			}
		}
		pos := fset.Position(decl.Pos()).Offset
		end := fset.Position(decl.End()).Offset
		if name == "" {
			prevEnd = end
			continue
		}
		for _, p := range outputDirectives {
			if !strings.HasPrefix(p.name, name) {
				continue
			}
			if p.offset < prevEnd || pos < p.offset {
				t.Errorf("directive %s does not appear before definition %s", p.text, name)
			}
		}
		prevEnd = end
	}
}

type directiveInfo struct {
	text   string // full text of the comment, not including newline
	name   string // text after //go:
	offset int    // byte offset of first slash in comment
}

func findDirectives(source []byte) []directiveInfo {
	var directives []directiveInfo
	directivePrefix := []byte("\n//go:")
	offset := 0
	for {
		i := bytes.Index(source[offset:], directivePrefix)
		if i < 0 {
			break
		}
		i++ // skip newline
		p := source[offset+i:]
		j := bytes.IndexByte(p, '\n')
		if j < 0 {
			// reached EOF
			j = len(p)
		}
		directive := directiveInfo{
			text:   string(p[:j]),
			name:   string(p[len(directivePrefix)-1 : j]),
			offset: offset + i,
		}
		directives = append(directives, directive)
		offset += i + j
	}
	return directives
}

// Makes sure that `cover -func=profile.cov` reports accurate coverage.
// Issue #20515.
func TestCoverFunc(t *testing.T) {
	// go tool cover -func ./testdata/profile.cov
	cmd := exec.Command(testenv.GoToolPath(t), "tool", "cover", "-func", coverProfile)
	out, err := cmd.Output()
	if err != nil {
		if ee, ok := err.(*exec.ExitError); ok {
			t.Logf("%s", ee.Stderr)
		}
		t.Fatal(err)
	}

	if got, err := regexp.Match(".*total:.*100.0.*", out); err != nil || !got {
		t.Logf("%s", out)
		t.Errorf("invalid coverage counts. got=(%v, %v); want=(true; nil)", got, err)
	}
}

// Check that cover produces correct HTML.
// Issue #25767.
func TestCoverHTML(t *testing.T) {
	testenv.MustHaveGoBuild(t)
	if !*debug {
		defer os.Remove(testcover)
		defer os.Remove(htmlProfile)
		defer os.Remove(htmlHTML)
	}
	// go build -o testcover
	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", testcover)
	run(cmd, t)
	// go test -coverprofile testdata/html/html.cov cmd/cover/testdata/html
	cmd = exec.Command(testenv.GoToolPath(t), "test", "-coverprofile", htmlProfile, "cmd/cover/testdata/html")
	run(cmd, t)
	// ./testcover -html testdata/html/html.cov -o testdata/html/html.html
	cmd = exec.Command(testcover, "-html", htmlProfile, "-o", htmlHTML)
	run(cmd, t)

	// Extract the parts of the HTML with comment markers,
	// and compare against a golden file.
	entireHTML, err := ioutil.ReadFile(htmlHTML)
	if err != nil {
		t.Fatal(err)
	}
	var out bytes.Buffer
	scan := bufio.NewScanner(bytes.NewReader(entireHTML))
	in := false
	for scan.Scan() {
		line := scan.Text()
		if strings.Contains(line, "// START") {
			in = true
		}
		if in {
			fmt.Fprintln(&out, line)
		}
		if strings.Contains(line, "// END") {
			in = false
		}
	}
	golden, err := ioutil.ReadFile(htmlGolden)
	if err != nil {
		t.Fatalf("reading golden file: %v", err)
	}
	// Ignore white space differences.
	// Break into lines, then compare by breaking into words.
	goldenLines := strings.Split(string(golden), "\n")
	outLines := strings.Split(out.String(), "\n")
	// Compare at the line level, stopping at first different line so
	// we don't generate tons of output if there's an inserted or deleted line.
	for i, goldenLine := range goldenLines {
		if i > len(outLines) {
			t.Fatalf("output shorter than golden; stops before line %d: %s\n", i+1, goldenLine)
		}
		// Convert all white space to simple spaces, for easy comparison.
		goldenLine = strings.Join(strings.Fields(goldenLine), " ")
		outLine := strings.Join(strings.Fields(outLines[i]), " ")
		if outLine != goldenLine {
			t.Fatalf("line %d differs: got:\n\t%s\nwant:\n\t%s", i+1, outLine, goldenLine)
		}
	}
	if len(goldenLines) != len(outLines) {
		t.Fatalf("output longer than golden; first extra output line %d: %q\n", len(goldenLines)+1, outLines[len(goldenLines)])
	}
}

func run(c *exec.Cmd, t *testing.T) {
	t.Helper()
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	err := c.Run()
	if err != nil {
		t.Fatal(err)
	}
}
<commit_msg>cmd/cover: fix off-by-one error in TestCoverHTML<commit_after>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main_test

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"internal/testenv"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strings"
	"testing"
)

const (
	// Data directory, also the package directory for the test.
	testdata = "testdata"

	// Binaries we compile.
	testcover = "./testcover.exe"
)

var (
	// Files we use.
	testMain     = filepath.Join(testdata, "main.go")
	testTest     = filepath.Join(testdata, "test.go")
	coverInput   = filepath.Join(testdata, "test_line.go")
	coverOutput  = filepath.Join(testdata, "test_cover.go")
	coverProfile = filepath.Join(testdata, "profile.cov")

	// The HTML test files are in a separate directory
	// so they are a complete package.
	htmlProfile = filepath.Join(testdata, "html", "html.cov")
	htmlHTML    = filepath.Join(testdata, "html", "html.html")
	htmlGolden  = filepath.Join(testdata, "html", "html.golden")
)

var debug = flag.Bool("debug", false, "keep rewritten files for debugging")

// Run this shell script, but do it in Go so it can be run by "go test".
//
//	replace the word LINE with the line number < testdata/test.go > testdata/test_line.go
// 	go build -o ./testcover
// 	./testcover -mode=count -var=CoverTest -o ./testdata/test_cover.go testdata/test_line.go
//	go run ./testdata/main.go ./testdata/test.go
//
func TestCover(t *testing.T) {
	testenv.MustHaveGoBuild(t)

	// Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
	file, err := ioutil.ReadFile(testTest)
	if err != nil {
		t.Fatal(err)
	}
	lines := bytes.Split(file, []byte("\n"))
	for i, line := range lines {
		lines[i] = bytes.Replace(line, []byte("LINE"), []byte(fmt.Sprint(i+1)), -1)
	}

	// Add a function that is not gofmt'ed. This used to cause a crash.
	// We don't put it in test.go because then we would have to gofmt it.
	// Issue 23927.
	lines = append(lines, []byte("func unFormatted() {"),
		[]byte("\tif true {"),
		[]byte("\t}else{"),
		[]byte("\t}"),
		[]byte("}"))
	lines = append(lines, []byte("func unFormatted2(b bool) {if b{}else{}}"))

	if err := ioutil.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666); err != nil {
		t.Fatal(err)
	}

	// defer removal of test_line.go
	if !*debug {
		defer os.Remove(coverInput)
	}

	// go build -o testcover
	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", testcover)
	run(cmd, t)

	// defer removal of testcover
	defer os.Remove(testcover)

	// ./testcover -mode=count -var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest -o ./testdata/test_cover.go testdata/test_line.go
	cmd = exec.Command(testcover, "-mode=count", "-var=thisNameMustBeVeryLongToCauseOverflowOfCounterIncrementStatementOntoNextLineForTest", "-o", coverOutput, coverInput)
	run(cmd, t)

	// defer removal of ./testdata/test_cover.go
	if !*debug {
		defer os.Remove(coverOutput)
	}

	// go run ./testdata/main.go ./testdata/test.go
	cmd = exec.Command(testenv.GoToolPath(t), "run", testMain, coverOutput)
	run(cmd, t)

	file, err = ioutil.ReadFile(coverOutput)
	if err != nil {
		t.Fatal(err)
	}
	// compiler directive must appear right next to function declaration.
	if got, err := regexp.MatchString(".*\n//go:nosplit\nfunc someFunction().*", string(file)); err != nil || !got {
		t.Error("misplaced compiler directive")
	}
	// "go:linkname" compiler directive should be present.
	if got, err := regexp.MatchString(`.*go\:linkname some\_name some\_name.*`, string(file)); err != nil || !got {
		t.Error("'go:linkname' compiler directive not found")
	}

	// Other comments should be preserved too.
	c := ".*// This comment didn't appear in generated go code.*"
	if got, err := regexp.MatchString(c, string(file)); err != nil || !got {
		t.Errorf("non compiler directive comment %q not found", c)
	}
}

// TestDirectives checks that compiler directives are preserved and positioned
// correctly. Directives that occur before top-level declarations should remain
// above those declarations, even if they are not part of the block of
// documentation comments.
func TestDirectives(t *testing.T) {
	// Read the source file and find all the directives. We'll keep
	// track of whether each one has been seen in the output.
	testDirectives := filepath.Join(testdata, "directives.go")
	source, err := ioutil.ReadFile(testDirectives)
	if err != nil {
		t.Fatal(err)
	}
	sourceDirectives := findDirectives(source)

	// go tool cover -mode=atomic ./testdata/directives.go
	cmd := exec.Command(testenv.GoToolPath(t), "tool", "cover", "-mode=atomic", testDirectives)
	cmd.Stderr = os.Stderr
	output, err := cmd.Output()
	if err != nil {
		t.Fatal(err)
	}

	// Check that all directives are present in the output.
	outputDirectives := findDirectives(output)
	foundDirective := make(map[string]bool)
	for _, p := range sourceDirectives {
		foundDirective[p.name] = false
	}
	for _, p := range outputDirectives {
		if found, ok := foundDirective[p.name]; !ok {
			t.Errorf("unexpected directive in output: %s", p.text)
		} else if found {
			t.Errorf("directive found multiple times in output: %s", p.text)
		}
		foundDirective[p.name] = true
	}
	for name, found := range foundDirective {
		if !found {
			t.Errorf("missing directive: %s", name)
		}
	}

	// Check that directives that start with the name of top-level declarations
	// come before the beginning of the named declaration and after the end
	// of the previous declaration.
	fset := token.NewFileSet()
	astFile, err := parser.ParseFile(fset, testDirectives, output, 0)
	if err != nil {
		t.Fatal(err)
	}

	prevEnd := 0
	for _, decl := range astFile.Decls {
		var name string
		switch d := decl.(type) {
		case *ast.FuncDecl:
			name = d.Name.Name
		case *ast.GenDecl:
			if len(d.Specs) == 0 {
				// An empty group declaration. We still want to check that
				// directives can be associated with it, so we make up a name
				// to match directives in the test data.
				name = "_empty"
			} else if spec, ok := d.Specs[0].(*ast.TypeSpec); ok {
				name = spec.Name.Name
			}
		}
		pos := fset.Position(decl.Pos()).Offset
		end := fset.Position(decl.End()).Offset
		if name == "" {
			prevEnd = end
			continue
		}
		for _, p := range outputDirectives {
			if !strings.HasPrefix(p.name, name) {
				continue
			}
			if p.offset < prevEnd || pos < p.offset {
				t.Errorf("directive %s does not appear before definition %s", p.text, name)
			}
		}
		prevEnd = end
	}
}

type directiveInfo struct {
	text   string // full text of the comment, not including newline
	name   string // text after //go:
	offset int    // byte offset of first slash in comment
}

func findDirectives(source []byte) []directiveInfo {
	var directives []directiveInfo
	directivePrefix := []byte("\n//go:")
	offset := 0
	for {
		i := bytes.Index(source[offset:], directivePrefix)
		if i < 0 {
			break
		}
		i++ // skip newline
		p := source[offset+i:]
		j := bytes.IndexByte(p, '\n')
		if j < 0 {
			// reached EOF
			j = len(p)
		}
		directive := directiveInfo{
			text:   string(p[:j]),
			name:   string(p[len(directivePrefix)-1 : j]),
			offset: offset + i,
		}
		directives = append(directives, directive)
		offset += i + j
	}
	return directives
}

// Makes sure that `cover -func=profile.cov` reports accurate coverage.
// Issue #20515.
func TestCoverFunc(t *testing.T) {
	// go tool cover -func ./testdata/profile.cov
	cmd := exec.Command(testenv.GoToolPath(t), "tool", "cover", "-func", coverProfile)
	out, err := cmd.Output()
	if err != nil {
		if ee, ok := err.(*exec.ExitError); ok {
			t.Logf("%s", ee.Stderr)
		}
		t.Fatal(err)
	}

	if got, err := regexp.Match(".*total:.*100.0.*", out); err != nil || !got {
		t.Logf("%s", out)
		t.Errorf("invalid coverage counts. got=(%v, %v); want=(true; nil)", got, err)
	}
}

// Check that cover produces correct HTML.
// Issue #25767.
func TestCoverHTML(t *testing.T) {
	testenv.MustHaveGoBuild(t)
	if !*debug {
		defer os.Remove(testcover)
		defer os.Remove(htmlProfile)
		defer os.Remove(htmlHTML)
	}
	// go build -o testcover
	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", testcover)
	run(cmd, t)
	// go test -coverprofile testdata/html/html.cov cmd/cover/testdata/html
	cmd = exec.Command(testenv.GoToolPath(t), "test", "-coverprofile", htmlProfile, "cmd/cover/testdata/html")
	run(cmd, t)
	// ./testcover -html testdata/html/html.cov -o testdata/html/html.html
	cmd = exec.Command(testcover, "-html", htmlProfile, "-o", htmlHTML)
	run(cmd, t)

	// Extract the parts of the HTML with comment markers,
	// and compare against a golden file.
	entireHTML, err := ioutil.ReadFile(htmlHTML)
	if err != nil {
		t.Fatal(err)
	}
	var out bytes.Buffer
	scan := bufio.NewScanner(bytes.NewReader(entireHTML))
	in := false
	for scan.Scan() {
		line := scan.Text()
		if strings.Contains(line, "// START") {
			in = true
		}
		if in {
			fmt.Fprintln(&out, line)
		}
		if strings.Contains(line, "// END") {
			in = false
		}
	}
	golden, err := ioutil.ReadFile(htmlGolden)
	if err != nil {
		t.Fatalf("reading golden file: %v", err)
	}
	// Ignore white space differences.
	// Break into lines, then compare by breaking into words.
	goldenLines := strings.Split(string(golden), "\n")
	outLines := strings.Split(out.String(), "\n")
	// Compare at the line level, stopping at first different line so
	// we don't generate tons of output if there's an inserted or deleted line.
	for i, goldenLine := range goldenLines {
		if i >= len(outLines) {
			t.Fatalf("output shorter than golden; stops before line %d: %s\n", i+1, goldenLine)
		}
		// Convert all white space to simple spaces, for easy comparison.
		goldenLine = strings.Join(strings.Fields(goldenLine), " ")
		outLine := strings.Join(strings.Fields(outLines[i]), " ")
		if outLine != goldenLine {
			t.Fatalf("line %d differs: got:\n\t%s\nwant:\n\t%s", i+1, outLine, goldenLine)
		}
	}
	if len(goldenLines) != len(outLines) {
		t.Fatalf("output longer than golden; first extra output line %d: %q\n", len(goldenLines)+1, outLines[len(goldenLines)])
	}
}

func run(c *exec.Cmd, t *testing.T) {
	t.Helper()
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	err := c.Run()
	if err != nil {
		t.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package fuse_nfs_certs

import (
	"os/exec"

	"code.cloudfoundry.org/lager"
	"code.cloudfoundry.org/lager/lagertest"

	"path/filepath"
	"syscall"

	"fmt"

	"os"

	"math/rand"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

const (
	PCAP uint32 = 200
)

var _ = Describe("Certify with: ", func() {
	var (
		testLogger lager.Logger
		err        error

		output []byte

		source     string
		mountPoint string

		pcapMountPath string
		rootMountPath string

		filename string
	)

	BeforeEach(func() {

		testLogger = lagertest.NewTestLogger("MainTest")

		source = os.Getenv("FUSE_MOUNT")
		Expect(source).NotTo(Equal(""))

		mountPoint = os.Getenv("NFS_MOUNT")
		Expect(source).NotTo(Equal(""))

		filename = randomString(10)
	})

	Context("given a pcap user with uid:gid 200:200", func() {
		BeforeEach(func() {
			output, err = asRoot(testLogger, "groupadd", "-g", fmt.Sprintf("%d", PCAP), "pcap")
			Expect(err).NotTo(HaveOccurred())

			output, err = asRoot(testLogger, "useradd", "-u", fmt.Sprintf("%d", PCAP), "-g", fmt.Sprintf("%d", PCAP), "pcap")
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			output, err = asRoot(testLogger, "userdel", "pcap")

			output, err = asRoot(testLogger, "groupdel", "pcap")
		})

		Context("given a fuse-nfs mount mapping pcap user to uid:gid 3000:3050", func() {
			BeforeEach(func() {
				// pcap mount
				pcapMountPath = filepath.Join("/tmp", "fuse_nfs_certs")
				output, err = asUser(testLogger, PCAP, PCAP, "mkdir", "-p", pcapMountPath)
				Expect(err).NotTo(HaveOccurred())

				output, err = asUser(testLogger, PCAP, PCAP, "fuse-nfs", "-n", source, "-m", fmt.Sprintf("%s?uid=3000&gid=3050", pcapMountPath))
				Expect(err).NotTo(HaveOccurred())

				// root mount
				rootMountPath = filepath.Join("/tmp", "fuse_nfs_certs_root")

				output, err = asRoot(testLogger, "mkdir", "-p", rootMountPath)
				Expect(err).NotTo(HaveOccurred())

				output, err = asRoot(testLogger, "mount", "-t", "nfs", "-o", "nfsvers=3,nolock", mountPoint, rootMountPath)
				Expect(err).NotTo(HaveOccurred())
			})

			AfterEach(func() {
				output, err = asUser(testLogger, PCAP, PCAP, "rm", filepath.Join(pcapMountPath, filename))
				Expect(err).NotTo(HaveOccurred())

				output, err = asRoot(testLogger, "umount", rootMountPath)
				Expect(err).NotTo(HaveOccurred())

				err = os.Remove(rootMountPath)
				Expect(err).ToNot(HaveOccurred())

				output, err = asRoot(testLogger, "umount", "-f", pcapMountPath)
				Expect(err).NotTo(HaveOccurred())

				err = os.Remove(pcapMountPath)
				Expect(err).ToNot(HaveOccurred())
			})

			It("successfully creates a file with uid:gid pcap:pcap", func() {
				output, err = asUser(testLogger, PCAP, PCAP, "touch", filepath.Join(pcapMountPath, filename))
				Expect(err).NotTo(HaveOccurred())

				output, err = asUser(testLogger, PCAP, PCAP, "stat", "-c", "%u:%g", filepath.Join(pcapMountPath, filename))
				Expect(err).NotTo(HaveOccurred())
				Expect(string(output)).To(Equal("200:200\n"))

				output, err = asUser(testLogger, PCAP, PCAP, "stat", "-c", "%u:%g", filepath.Join(rootMountPath, filename))
				Expect(err).NotTo(HaveOccurred())
				Expect(string(output)).To(Equal("3000:3050\n"))
			})
		})
	})
})

func asUser(logger lager.Logger, uid, gid uint32, cmd string, args ...string) ([]byte, error) {
	logger.Info(fmt.Sprintf("Executing command %s %#v", cmd, args))
	cmdHandle := exec.Command(cmd, args...)

	attrs := syscall.SysProcAttr{
		Credential: &syscall.Credential{
			Uid: uid,
			Gid: gid,
		},
	}
	cmdHandle.SysProcAttr = &attrs

	output, err := cmdHandle.CombinedOutput()
	if err != nil {
		logger.Error(string(output), err)
	}

	return output, err
}

func asRoot(logger lager.Logger, cmd string, args ...string) ([]byte, error) {
	return asUser(logger, 0, 0, cmd, args...)
}

func randomString(n int) string {
	runes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

	b := make([]rune, n)
	for i := range b {
		b[i] = runes[rand.Intn(len(runes))]
	}
	return string(b)
}
<commit_msg>Add the uid/gid params to the source, not the mount path<commit_after>package fuse_nfs_certs

import (
	"os/exec"

	"code.cloudfoundry.org/lager"
	"code.cloudfoundry.org/lager/lagertest"

	"path/filepath"
	"syscall"

	"fmt"

	"os"

	"math/rand"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

const (
	PCAP uint32 = 200
)

var _ = Describe("Certify with: ", func() {
	var (
		testLogger lager.Logger
		err        error

		output []byte

		source     string
		mountPoint string

		pcapMountPath string
		rootMountPath string

		filename string
	)

	BeforeEach(func() {

		testLogger = lagertest.NewTestLogger("MainTest")

		source = os.Getenv("FUSE_MOUNT")
		Expect(source).NotTo(Equal(""))

		mountPoint = os.Getenv("NFS_MOUNT")
		Expect(source).NotTo(Equal(""))

		filename = randomString(10)
	})

	Context("given a pcap user with uid:gid 200:200", func() {
		BeforeEach(func() {
			output, err = asRoot(testLogger, "groupadd", "-g", fmt.Sprintf("%d", PCAP), "pcap")
			Expect(err).NotTo(HaveOccurred())

			output, err = asRoot(testLogger, "useradd", "-u", fmt.Sprintf("%d", PCAP), "-g", fmt.Sprintf("%d", PCAP), "pcap")
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			output, err = asRoot(testLogger, "userdel", "pcap")

			output, err = asRoot(testLogger, "groupdel", "pcap")
		})

		Context("given a fuse-nfs mount mapping pcap user to uid:gid 3000:3050", func() {
			BeforeEach(func() {
				// pcap mount
				pcapMountPath = filepath.Join("/tmp", "fuse_nfs_certs")
				output, err = asUser(testLogger, PCAP, PCAP, "mkdir", "-p", pcapMountPath)
				Expect(err).NotTo(HaveOccurred())

				output, err = asUser(testLogger, PCAP, PCAP, "fuse-nfs", "-n", fmt.Sprintf("%s?uid=3000&gid=3050", source), "-m", pcapMountPath)
				Expect(err).NotTo(HaveOccurred())

				// root mount
				rootMountPath = filepath.Join("/tmp", "fuse_nfs_certs_root")

				output, err = asRoot(testLogger, "mkdir", "-p", rootMountPath)
				Expect(err).NotTo(HaveOccurred())

				output, err = asRoot(testLogger, "mount", "-t", "nfs", "-o", "nfsvers=3,nolock", mountPoint, rootMountPath)
				Expect(err).NotTo(HaveOccurred())
			})

			AfterEach(func() {
				output, err = asUser(testLogger, PCAP, PCAP, "rm", filepath.Join(pcapMountPath, filename))
				Expect(err).NotTo(HaveOccurred())

				output, err = asRoot(testLogger, "umount", rootMountPath)
				Expect(err).NotTo(HaveOccurred())

				err = os.Remove(rootMountPath)
				Expect(err).ToNot(HaveOccurred())

				output, err = asRoot(testLogger, "umount", "-f", pcapMountPath)
				Expect(err).NotTo(HaveOccurred())

				err = os.Remove(pcapMountPath)
				Expect(err).ToNot(HaveOccurred())
			})

			It("successfully creates a file with uid:gid pcap:pcap", func() {
				output, err = asUser(testLogger, PCAP, PCAP, "touch", filepath.Join(pcapMountPath, filename))
				Expect(err).NotTo(HaveOccurred())

				output, err = asUser(testLogger, PCAP, PCAP, "stat", "-c", "%u:%g", filepath.Join(pcapMountPath, filename))
				Expect(err).NotTo(HaveOccurred())
				Expect(string(output)).To(Equal("200:200\n"))

				output, err = asUser(testLogger, PCAP, PCAP, "stat", "-c", "%u:%g", filepath.Join(rootMountPath, filename))
				Expect(err).NotTo(HaveOccurred())
				Expect(string(output)).To(Equal("3000:3050\n"))
			})
		})
	})
})

func asUser(logger lager.Logger, uid, gid uint32, cmd string, args ...string) ([]byte, error) {
	logger.Info(fmt.Sprintf("Executing command %s %#v", cmd, args))
	cmdHandle := exec.Command(cmd, args...)

	attrs := syscall.SysProcAttr{
		Credential: &syscall.Credential{
			Uid: uid,
			Gid: gid,
		},
	}
	cmdHandle.SysProcAttr = &attrs

	output, err := cmdHandle.CombinedOutput()
	if err != nil {
		logger.Error(string(output), err)
	}

	return output, err
}

func asRoot(logger lager.Logger, cmd string, args ...string) ([]byte, error) {
	return asUser(logger, 0, 0, cmd, args...)
}

func randomString(n int) string {
	runes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

	b := make([]rune, n)
	for i := range b {
		b[i] = runes[rand.Intn(len(runes))]
	}
	return string(b)
}
<|endoftext|>


<commit_before>package skiplist

import "sync/atomic"

type Iterator struct {
	cmp        CompareFn
	s          *Skiplist
	prev, curr *Node
	valid      bool
	buf        *ActionBuffer
	deleted    bool
}

func (s *Skiplist) NewIterator(cmp CompareFn,
	buf *ActionBuffer) *Iterator {

	return &Iterator{
		cmp: cmp,
		s:   s,
		buf: buf,
	}
}

func (it *Iterator) SeekFirst() {
	it.prev = it.s.head
	it.curr, _ = it.s.head.getNext(0)
	it.valid = true
}

func (it *Iterator) Seek(itm Item) bool {
	it.valid = true
	found := it.s.FindPath(itm, it.cmp, it.buf) != nil
	it.prev = it.buf.preds[0]
	it.curr = it.buf.succs[0]
	return found
}

func (it *Iterator) Valid() bool {
	if it.valid && it.curr == it.s.tail {
		it.valid = false
	}

	return it.valid
}

func (it *Iterator) Get() Item {
	return it.curr.Item()
}

func (it *Iterator) GetNode() *Node {
	return it.curr
}

func (it *Iterator) Delete() {
	it.s.softDelete(it.curr)
	// It will observe that current item is deleted
	// Run delete helper and move to the next possible item
	it.Next()
	it.deleted = true
}

func (it *Iterator) Next() {
	if it.deleted {
		it.deleted = false
		return
	}

retry:
	it.valid = true
	next, deleted := it.curr.getNext(0)
	for deleted {
		if !it.s.helpDelete(0, it.prev, it.curr, next) {
			atomic.AddUint64(&it.s.stats.readConflicts, 1)
			found := it.s.FindPath(it.curr.Item(), it.cmp, it.buf) != nil
			last := it.curr
			it.prev = it.buf.preds[0]
			it.curr = it.buf.succs[0]
			if found && last == it.curr {
				goto retry
			} else {
				return
			}
		}
		it.curr, _ = it.prev.getNext(0)
		next, deleted = it.curr.getNext(0)
	}

	it.prev = it.curr
	it.curr = next
}
<commit_msg>MB-16961 skiplist: Fix iterator.Next() causing missing items<commit_after>package skiplist

import "sync/atomic"

type Iterator struct {
	cmp        CompareFn
	s          *Skiplist
	prev, curr *Node
	valid      bool
	buf        *ActionBuffer
	deleted    bool
}

func (s *Skiplist) NewIterator(cmp CompareFn,
	buf *ActionBuffer) *Iterator {

	return &Iterator{
		cmp: cmp,
		s:   s,
		buf: buf,
	}
}

func (it *Iterator) SeekFirst() {
	it.prev = it.s.head
	it.curr, _ = it.s.head.getNext(0)
	it.valid = true
}

func (it *Iterator) Seek(itm Item) bool {
	it.valid = true
	found := it.s.FindPath(itm, it.cmp, it.buf) != nil
	it.prev = it.buf.preds[0]
	it.curr = it.buf.succs[0]
	return found
}

func (it *Iterator) Valid() bool {
	if it.valid && it.curr == it.s.tail {
		it.valid = false
	}

	return it.valid
}

func (it *Iterator) Get() Item {
	return it.curr.Item()
}

func (it *Iterator) GetNode() *Node {
	return it.curr
}

func (it *Iterator) Delete() {
	it.s.softDelete(it.curr)
	// It will observe that current item is deleted
	// Run delete helper and move to the next possible item
	it.Next()
	it.deleted = true
}

func (it *Iterator) Next() {
	if it.deleted {
		it.deleted = false
		return
	}

retry:
	it.valid = true
	next, deleted := it.curr.getNext(0)
	if deleted {
		// Current node is deleted. Unlink current node from the level
		// and make next node as current node.
		// If it fails, refresh the path buffer and obtain new current node.
		if it.s.helpDelete(0, it.prev, it.curr, next) {
			it.curr = next
		} else {
			atomic.AddUint64(&it.s.stats.readConflicts, 1)
			found := it.s.FindPath(it.curr.Item(), it.cmp, it.buf) != nil
			last := it.curr
			it.prev = it.buf.preds[0]
			it.curr = it.buf.succs[0]
			if found && last == it.curr {
				goto retry
			}
		}
	} else {
		it.prev = it.curr
		it.curr = next
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/nlopes/slack"
	"os"
	"time"
)

var apiKey string = ""

var slackPolicy = "To use Slack you must have 2FA enabled as per the requirements and terms of use.  You will be reminded every 24 hours until you enabled 2FA.\n\n" +
	"Every so often we will disable accounts that do not have 2FA turned on.  To avoid this please turn on 2FA now.  Instructions for 2FA can be found at https://slack.zendesk.com/hc/en-us/articles/204509068-Enabling-two-factor-authentication.\n\n" +
	"You can turn on 2FA at https://cloudplatform.slack.com/account/".

func sendMessage(api slack.Client, channel string, message string) error {
	params := slack.PostMessageParameters{}
	params.LinkNames = 1
	_, _, err := api.PostMessage(channel, message, params)
	return err
}

func annoyUser(api slack.Client, user string) error {
	message := "You have been identied as a user that does not have 2 Factor Auth (2FA).\n\n" + slackPolicy

	err := sendMessage(api, "@"+user, message)
	return err
}

func shameUsers(api slack.Client, userString string) error {
	message := "Hello everyone...\n\n" +
		"The following users have not enabled 2 Factor auth " + userString + "\n\n" +
		slackPolicy

	err := sendMessage(api, "#general", message)
	return err
}

func getUsers(api slack.Client) error {
	users, err := api.GetUsers()
	if err != nil {
		return err
	}

	var userList = ""

	for _, member := range users {
		if member.Has2FA == false && member.Deleted == false && member.IsBot == false {
			if err := annoyUser(api, member.Name); err != nil {
				return err
			}
			userList += "@" + member.Name + " "
			fmt.Printf("%s\n", member.Name)
		}
	}

	if err := shameUsers(api, userList); err != nil {
		return err
	}

	return nil
}

func main() {

	apiKey = os.Getenv("SLACK_API_KEY")
	if apiKey == "" {
		println("You must set the enviroment variable SLACK_API_KEY")
		os.Exit(-1)
	}

	api := slack.New(apiKey)

	for {
		if err := getUsers(*api); err != nil {
			fmt.Printf("Error getting users and annoying them %s\n", err)
		}
		fmt.Printf("Running again in 24 hours...")
		time.Sleep(time.Hour * 24)
	}

}
<commit_msg>build fix<commit_after>package main

import (
	"fmt"
	"github.com/nlopes/slack"
	"os"
	"time"
)

var apiKey string = ""

var slackPolicy = "To use Slack you must have 2FA enabled as per the requirements and terms of use.  You will be reminded every 24 hours until you enabled 2FA.\n\n" +
	"Every so often we will disable accounts that do not have 2FA turned on.  To avoid this please turn on 2FA now.  Instructions for 2FA can be found at https://slack.zendesk.com/hc/en-us/articles/204509068-Enabling-two-factor-authentication.\n\n" +
	"You can turn on 2FA at https://cloudplatform.slack.com/account/"

func sendMessage(api slack.Client, channel string, message string) error {
	params := slack.PostMessageParameters{}
	params.LinkNames = 1
	_, _, err := api.PostMessage(channel, message, params)
	return err
}

func annoyUser(api slack.Client, user string) error {
	message := "You have been identied as a user that does not have 2 Factor Auth (2FA).\n\n" + slackPolicy

	err := sendMessage(api, "@"+user, message)
	return err
}

func shameUsers(api slack.Client, userString string) error {
	message := "Hello everyone...\n\n" +
		"The following users have not enabled 2 Factor auth " + userString + "\n\n" +
		slackPolicy

	err := sendMessage(api, "#general", message)
	return err
}

func getUsers(api slack.Client) error {
	users, err := api.GetUsers()
	if err != nil {
		return err
	}

	var userList = ""

	for _, member := range users {
		if member.Has2FA == false && member.Deleted == false && member.IsBot == false {
			if err := annoyUser(api, member.Name); err != nil {
				return err
			}
			userList += "@" + member.Name + " "
			fmt.Printf("%s\n", member.Name)
		}
	}

	if err := shameUsers(api, userList); err != nil {
		return err
	}

	return nil
}

func main() {

	apiKey = os.Getenv("SLACK_API_KEY")
	if apiKey == "" {
		println("You must set the enviroment variable SLACK_API_KEY")
		os.Exit(-1)
	}

	api := slack.New(apiKey)

	for {
		if err := getUsers(*api); err != nil {
			fmt.Printf("Error getting users and annoying them %s\n", err)
		}
		fmt.Printf("Running again in 24 hours...")
		time.Sleep(time.Hour * 24)
	}

}
<|endoftext|>


<commit_before>package slack

// SlashCommand represents a slash command from Slack
type SlashCommand struct {
}
<commit_msg>add properties to slashcommand struct<commit_after>package slack

// SlashCommand represents a slash command from Slack
type SlashCommand struct {
	Token          string `form:"token"`
	TeamID         string `form:"team_id"`
	TeamDomain     string `form:"team_domain"`
	EnterpriseID   string `form:"enterprise_id"`
	EnterpriseName string `form:"enterprise_name"`
	ChannelID      string `form:"channel_id"`
	ChannelName    string `form:"channel_name"`
	UserID         string `form:"user_id"`
	UserName       string `form:"user_name"`
	Command        string `form:"command"`
	Text           string `form:"text"`
	ResponseURL    string `form:"response_url"`
}
<|endoftext|>


<commit_before>package main

import (
	"testing"
	"net/http"
	"net/http/httptest"
	//"encoding/json"
	"os"
)

func TestSetUp(t *testing.T)  {
	err := SetUp()
	if(err != nil) {
		t.Fatalf("Setup fialed with error: %v", err.Error())
	}
}

func TestFileList(test *testing.T) {
	err := SetUp()
	if(err != nil) {
		test.Fatalf("Setup fialed with error: %v", err.Error())
	}
	request, _ := http.NewRequest("GET", "/stl", nil)
	writer := httptest.NewRecorder()
	fileListHandler(writer, request)
	if writer.Code != http.StatusOK {
		test.Errorf("File list didn't return OK, returned: %v", writer.Code)
	}
	/*var fileList []string
	if err = json.Unmarshal(writer.Body.Bytes(), fileList); err != nil {
		test.Errorf("File list didn't return valid json, returned: %v", writer.Body.String())
	}*/

	request, _ = http.NewRequest("GET", "/gcode", nil)
	writer = httptest.NewRecorder()
	fileListHandler(writer, request)
	if writer.Code != http.StatusOK {
		test.Errorf("File list didn't return OK, returned: %v", writer.Code)
	}
	/*if err = json.Unmarshal(writer.Body.Bytes(), fileList); err != nil {
		test.Errorf("File list didn't return valid json, returned: %v", writer.Body.String())
	}*/
}

func TestDeleteFile(test *testing.T) {
	err := SetUp()
	if err != nil {
		test.Fatalf("Setup fialed with error: %v", err.Error())
	}

	_, err = os.Create("./stl/test.stl")
	if err != nil {
		test.Errorf("Failed to creat test stl file: %v", err)
	}
	request, _ := http.NewRequest("DELETE", "", nil)
	writer := httptest.NewRecorder()
	_getVars := getVars
	getVars = func(_ *http.Request) map[string]string{
		return map[string]string {
			"type": "stl",
			"name": "test.stl",
		}
	}
	deleteFileHandler(writer, request)
	if writer.Code != http.StatusNoContent {
		test.Errorf("File list didn't return NoContent, returned: %v", writer.Code)
	}
	if _, err := os.Stat("./stl/test.stl"); err == nil {
		test.Error("STL file was not deleted")
	}

	_, err = os.Create("./gcode/test.gcode")
	if err != nil {
		test.Errorf("Failed to creat test gcode file: %v", err)
	}
	request, _ = http.NewRequest("DELETE", "", nil)
	writer = httptest.NewRecorder()
	getVars = func(_ *http.Request) map[string]string{
		return map[string]string {
			"type": "gcode",
			"name": "test.gcode",
		}
	}
	deleteFileHandler(writer, request)
	if writer.Code != http.StatusNoContent {
		test.Errorf("File list didn't return NoContent, returned: %v", writer.Code)
	}
	if _, err := os.Stat("./gcode/test.gcode"); err == nil {
		test.Error("Gcode file was not deleted")
	}
	getVars = _getVars
}<commit_msg>Added clear files handler<commit_after>package main

import (
	"testing"
	"net/http"
	"net/http/httptest"
	//"encoding/json"
	"os"
)

func TestSetUp(t *testing.T)  {
	err := SetUp()
	if(err != nil) {
		t.Fatalf("Setup failed with error: %v", err.Error())
	}
}

func TestFileList(test *testing.T) {
	err := SetUp()
	if(err != nil) {
		test.Fatalf("Setup fialed with error: %v", err.Error())
	}
	request, _ := http.NewRequest("GET", "/stl", nil)
	writer := httptest.NewRecorder()
	fileListHandler(writer, request)
	if writer.Code != http.StatusOK {
		test.Errorf("File list didn't return OK, returned: %v", writer.Code)
	}
	/*var fileList []string
	if err = json.Unmarshal(writer.Body.Bytes(), fileList); err != nil {
		test.Errorf("File list didn't return valid json, returned: %v", writer.Body.String())
	}*/

	request, _ = http.NewRequest("GET", "/gcode", nil)
	writer = httptest.NewRecorder()
	fileListHandler(writer, request)
	if writer.Code != http.StatusOK {
		test.Errorf("File list didn't return OK, returned: %v", writer.Code)
	}
	/*if err = json.Unmarshal(writer.Body.Bytes(), fileList); err != nil {
		test.Errorf("File list didn't return valid json, returned: %v", writer.Body.String())
	}*/
}

func TestDeleteFile(test *testing.T) {
	err := SetUp()
	if err != nil {
		test.Fatalf("Setup failed with error: %v", err.Error())
	}

	_, err = os.Create("./stl/test.stl")
	if err != nil {
		test.Errorf("Failed to creat test stl file: %v", err)
	}
	request, _ := http.NewRequest("DELETE", "", nil)
	writer := httptest.NewRecorder()
	_getVars := getVars
	getVars = func(_ *http.Request) map[string]string{
		return map[string]string {
			"type": "stl",
			"name": "test.stl",
		}
	}
	deleteFileHandler(writer, request)
	if writer.Code != http.StatusNoContent {
		test.Errorf("File list didn't return NoContent, returned: %v", writer.Code)
	}
	if _, err := os.Stat("./stl/test.stl"); err == nil {
		test.Error("STL file was not deleted")
	}

	_, err = os.Create("./gcode/test.gcode")
	if err != nil {
		test.Errorf("Failed to creat test gcode file: %v", err)
	}
	request, _ = http.NewRequest("DELETE", "", nil)
	writer = httptest.NewRecorder()
	getVars = func(_ *http.Request) map[string]string{
		return map[string]string {
			"type": "gcode",
			"name": "test.gcode",
		}
	}
	deleteFileHandler(writer, request)
	if writer.Code != http.StatusNoContent {
		test.Errorf("File list didn't return NoContent, returned: %v", writer.Code)
	}
	if _, err := os.Stat("./gcode/test.gcode"); err == nil {
		test.Error("Gcode file was not deleted")
	}
	getVars = _getVars
}

func TestClearFiles(test *testing.T) {
	err := SetUp()
	if err != nil {
		test.Fatalf("Setup failed with error: %v", err.Error())
	}

	_, err = os.Create("./stl/test.stl")
	if err != nil {
		test.Errorf("Failed to creat test stl file: %v", err)
	}
	_, err = os.Create("./stl/test2.stl")
	if err != nil {
		test.Errorf("Failed to creat test stl file: %v", err)
	}
	request, _ := http.NewRequest("DELETE", "/stl", nil)
	writer := httptest.NewRecorder()
	clearFilesHandler(writer, request)
	if writer.Code != http.StatusNoContent {
		test.Errorf("File list didn't return NoContent, returned: %v", writer.Code)
	}
	if _, err := os.Stat("./stl/test.stl"); err == nil {
		test.Error("STL file was not deleted")
	}
	if _, err := os.Stat("./stl/test2.stl"); err == nil {
		test.Error("STL file was not deleted")
	}

	_, err = os.Create("./gcode/test.gcode")
	if err != nil {
		test.Errorf("Failed to creat test gcode file: %v", err)
	}
	_, err = os.Create("./gcode/test2.gcode")
	if err != nil {
		test.Errorf("Failed to creat test gcode file: %v", err)
	}
	request, _ = http.NewRequest("DELETE", "/gcode", nil)
	writer = httptest.NewRecorder()
	clearFilesHandler(writer, request)
	if writer.Code != http.StatusNoContent {
		test.Errorf("File list didn't return NoContent, returned: %v", writer.Code)
	}
	if _, err := os.Stat("./gcode/test.gcode"); err == nil {
		test.Error("Gcode file was not deleted")
	}
	if _, err := os.Stat("./gcode/test2.gcode"); err == nil {
		test.Error("Gcode file was not deleted")
	}
}<|endoftext|>


<commit_before>// Copyright ©2020 The go-hep Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package f64s provides common operations on float64 slices.
package f64s

import (
	"fmt"
)

var (
	errLength           = fmt.Errorf("f64s: length mismatch")
	errSortedIndices    = fmt.Errorf("f64s: indices not sorted")
	errDuplicateIndices = fmt.Errorf("f64s: duplicate indices")
)

// Filter creates a slice with all the elements x_i of src for which f(x_i) is true.
// Filter uses dst as work buffer, storing elements at the start of the slice.
// Filter clears dst if a slice is passed, and allocates a new slice if dst is nil.
func Filter(dst, src []float64, f func(v float64) bool) []float64 {

	if dst == nil {
		dst = make([]float64, 0, len(src))
	}

	dst = dst[:0]
	for _, x := range src {
		if f(x) {
			dst = append(dst, x)
		}
	}

	return dst
}

// Map creates a slice with all the elements f(x_i) where x_i are elements from src.
// Map uses dst as work buffer, storing elements at the start of the slice.
// Map allocates a new slice if dst is nil.
// Map will panic if the lengths of src and dst differ.
func Map(dst, src []float64, f func(v float64) float64) []float64 {

	if dst == nil {
		dst = make([]float64, len(src))
	}

	if len(src) != len(dst) {
		panic(errLength)
	}

	for i, x := range src {
		dst[i] = f(x)
	}
	return dst
}

// Find creates a slice with all indices corresponding to elements for which f(x) is true.
// Find uses dst as work buffer, storing indices at the start of the slice.
// Find clears dst if a slice is passed, and allocates a new slice if dst is nil.
func Find(dst []int, src []float64, f func(v float64) bool) []int {

	if dst == nil {
		dst = make([]int, 0, len(src))
	}

	dst = dst[:0]
	for i, x := range src {
		if f(x) {
			dst = append(dst, i)
		}
	}

	return dst
}

// Take creates a sub-slice of src with all elements indiced by the provided indices.
// Take uses dst as work buffer, storing elements at the start of the slice.
// Take clears dst if a slice is passed, and allocates a new slice if dst is nil.
// Take will panic if indices is not sorted or has duplicates.
// Take will panic if length of indices is larger than length of src.
// Take will panic if length of indices is different from length of dst.
func Take(dst, src []float64, indices []int) []float64 {

	if len(indices) > len(src) {
		panic(errLength)
	}

	if dst == nil {
		dst = make([]float64, len(indices))
	}

	if len(dst) != len(indices) {
		panic(errLength)
	}

	if len(indices) == 0 {
		return dst
	}

	dst[0] = src[indices[0]]
	for i := 1; i < len(indices); i++ {
		v0 := indices[i-1]
		v1 := indices[i]
		switch {
		case v0 == v1:
			panic(errDuplicateIndices)
		case v0 > v1:
			panic(errSortedIndices)
		}
		dst[i] = src[v1]
	}

	return dst
}
<commit_msg>sliceop/f64s: improve Take performances<commit_after>// Copyright ©2020 The go-hep Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package f64s provides common operations on float64 slices.
package f64s

import (
	"fmt"
)

var (
	errLength           = fmt.Errorf("f64s: length mismatch")
	errSortedIndices    = fmt.Errorf("f64s: indices not sorted")
	errDuplicateIndices = fmt.Errorf("f64s: duplicate indices")
)

// Filter creates a slice with all the elements x_i of src for which f(x_i) is true.
// Filter uses dst as work buffer, storing elements at the start of the slice.
// Filter clears dst if a slice is passed, and allocates a new slice if dst is nil.
func Filter(dst, src []float64, f func(v float64) bool) []float64 {

	if dst == nil {
		dst = make([]float64, 0, len(src))
	}

	dst = dst[:0]
	for _, x := range src {
		if f(x) {
			dst = append(dst, x)
		}
	}

	return dst
}

// Map creates a slice with all the elements f(x_i) where x_i are elements from src.
// Map uses dst as work buffer, storing elements at the start of the slice.
// Map allocates a new slice if dst is nil.
// Map will panic if the lengths of src and dst differ.
func Map(dst, src []float64, f func(v float64) float64) []float64 {

	if dst == nil {
		dst = make([]float64, len(src))
	}

	if len(src) != len(dst) {
		panic(errLength)
	}

	for i, x := range src {
		dst[i] = f(x)
	}
	return dst
}

// Find creates a slice with all indices corresponding to elements for which f(x) is true.
// Find uses dst as work buffer, storing indices at the start of the slice.
// Find clears dst if a slice is passed, and allocates a new slice if dst is nil.
func Find(dst []int, src []float64, f func(v float64) bool) []int {

	if dst == nil {
		dst = make([]int, 0, len(src))
	}

	dst = dst[:0]
	for i, x := range src {
		if f(x) {
			dst = append(dst, i)
		}
	}

	return dst
}

// Take creates a sub-slice of src with all elements indiced by the provided indices.
// Take uses dst as work buffer, storing elements at the start of the slice.
// Take clears dst if a slice is passed, and allocates a new slice if dst is nil.
// Take will panic if indices is not sorted or has duplicates.
// Take will panic if length of indices is larger than length of src.
// Take will panic if length of indices is different from length of dst.
func Take(dst, src []float64, indices []int) []float64 {

	if len(indices) > len(src) {
		panic(errLength)
	}

	if dst == nil {
		dst = make([]float64, len(indices))
	}

	if len(dst) != len(indices) {
		panic(errLength)
	}

	if len(indices) == 0 {
		return dst
	}

	dst[0] = src[indices[0]]
	for i := 1; i < len(indices); i++ {
		v0 := indices[i-1]
		v1 := indices[i]
		switch {
		case v0 < v1:
			// ok.
		case v0 == v1:
			panic(errDuplicateIndices)
		case v0 > v1:
			panic(errSortedIndices)
		}
		dst[i] = src[v1]
	}

	return dst
}
<|endoftext|>


<commit_before>package issue

import (
	"context"
	"errors"
	"log"
	"time"
)

func NewPoller(workerNum int) *Poller {
	return &Poller{
		routineGroup: newRoutineGroup(),
		workerNum:    workerNum,
	}
}

type Poller struct {
	routineGroup *routineGroup
	workerNum    int
}

func (p *Poller) Poll(ctx context.Context) error {
	for i := 0; i < p.workerNum; i++ {
		p.routineGroup.Run(func() {
			for {
				select {
				case <-ctx.Done():
					return
				default:
					task, err := p.fetch(ctx)
					if err != nil {
						log.Println("can't get task", err.Error())
						break
					}

					if err := p.execute(ctx, task); err != nil {
						log.Println("execute task error:", err.Error())
					}
				}
			}
		})
	}
	p.routineGroup.Wait()
	return nil
}

func (p *Poller) fetch(ctx context.Context) (string, error) {
	// connect database or other service
	time.Sleep(1000 * time.Millisecond)
	return "foobar", errors.New("no task")
}

func (p *Poller) execute(ctx context.Context, task string) error {
	return nil
}
<commit_msg>chore(example50): add steps<commit_after>package issue

import (
	"context"
	"errors"
	"log"
	"time"
)

func NewPoller(workerNum int) *Poller {
	return &Poller{
		routineGroup: newRoutineGroup(),
		workerNum:    workerNum,
	}
}

type Poller struct {
	routineGroup *routineGroup
	workerNum    int
}

func (p *Poller) Poll(ctx context.Context) error {
	for i := 0; i < p.workerNum; i++ {
		// step01
		p.routineGroup.Run(func() {
			for {
				select {
				case <-ctx.Done():
					return
				default:
					// step02
					task, err := p.fetch(ctx)
					if err != nil {
						log.Println("can't get task", err.Error())
						break
					}

					// step03
					if err := p.execute(ctx, task); err != nil {
						log.Println("execute task error:", err.Error())
					}
				}
			}
		})
	}
	p.routineGroup.Wait()
	return nil
}

func (p *Poller) fetch(ctx context.Context) (string, error) {
	// connect database or other service
	time.Sleep(1000 * time.Millisecond)
	return "foobar", errors.New("no task")
}

func (p *Poller) execute(ctx context.Context, task string) error {
	return nil
}
<|endoftext|>


<commit_before>package dna

import (
	"reflect"
	"testing"
)

var tallyTests = []struct {
	strand     DNA
	nucleotide byte
	expected   int
}{
	{"", 'A', 0},
	{"ACT", 'G', 0},
	{"CCCCC", 'C', 5},
	{"GGGGGTAACCCGG", 'T', 1},
}

func TestNucleotideCounts(t *testing.T) {
	for _, tt := range tallyTests {
		if count, err := tt.strand.Count(tt.nucleotide); err != nil {
			t.Fatal(err)
		} else if count != tt.expected {
			t.Fatalf("Got \"%v\", expected \"%v\"", count, tt.expected)
		}
	}
}

func TestHasErrorForInvalidNucleotides(t *testing.T) {
	dna := DNA("GATTACA")
	if _, err := dna.Count('X'); err == nil {
		t.Fatalf("X is an invalid nucleotide, but no error was raised")
	}
}

// In most cases, this test is pointless.
// Very occasionally it matters.
// Just roll with it.
func TestCountingDoesntChangeCount(t *testing.T) {
	dna := DNA("CGATTGGG")
	dna.Count('T')
	count1, err := dna.Count('T')
	if err != nil {
		t.Fatal(err)
	}
	count2, err := dna.Count('T')
	if err != nil {
		t.Fatal(err)
	}
	if count1 != count2 || count2 != 2 {
		t.Fatalf("Got %v, expected %v", []int{count1, count2}, []int{2, 2})
	}
}

type histogramTest struct {
	strand   DNA
	expected Histogram
	err      bool
}

var histogramTests = []histogramTest{
	{
		"",
		Histogram{'A': 0, 'C': 0, 'T': 0, 'G': 0},
		false,
	},
	{
		"GGGGGGGG",
		Histogram{'A': 0, 'C': 0, 'T': 0, 'G': 8},
		false,
	},
	{
		"AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC",
		Histogram{'A': 20, 'C': 12, 'T': 21, 'G': 17},
		false,
	},
	{
		"GGXXX",
		nil,
		true,
	},
}

func TestSequenceHistograms(t *testing.T) {
	for _, tt := range histogramTests {
		counts, err := tt.strand.Counts()
		if tt.err && err == nil {
			t.Fatalf("DNA{ %q }: expected error but didn't get one.", tt.strand)
		} else if !tt.err && err != nil {
			t.Fatalf("DNA{ %q }: expected no error but got error %s", tt.strand, err.Error())
		} else if !tt.err && !reflect.DeepEqual(counts, tt.expected) {
			t.Fatalf("DNA{ %q }: Got %v, expected %v", tt.strand, counts, tt.expected)
		}
	}
}

func BenchmarkSequenceHistograms(b *testing.B) {
	b.StopTimer()
	for _, tt := range histogramTests {
		for i := 0; i < b.N; i++ {
			b.StartTimer()

			tt.strand.Counts()

			b.StopTimer()
		}
	}
}

const targetTestVersion = 2

func TestTestVersion(t *testing.T) {
	if testVersion != targetTestVersion {
		t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
	}
}
<commit_msg>nucleotide-count: Decrease benchmark test runtime (#453)<commit_after>package dna

import (
	"reflect"
	"testing"
)

var tallyTests = []struct {
	strand     DNA
	nucleotide byte
	expected   int
}{
	{"", 'A', 0},
	{"ACT", 'G', 0},
	{"CCCCC", 'C', 5},
	{"GGGGGTAACCCGG", 'T', 1},
}

func TestNucleotideCounts(t *testing.T) {
	for _, tt := range tallyTests {
		if count, err := tt.strand.Count(tt.nucleotide); err != nil {
			t.Fatal(err)
		} else if count != tt.expected {
			t.Fatalf("Got \"%v\", expected \"%v\"", count, tt.expected)
		}
	}
}

func TestHasErrorForInvalidNucleotides(t *testing.T) {
	dna := DNA("GATTACA")
	if _, err := dna.Count('X'); err == nil {
		t.Fatalf("X is an invalid nucleotide, but no error was raised")
	}
}

// In most cases, this test is pointless.
// Very occasionally it matters.
// Just roll with it.
func TestCountingDoesntChangeCount(t *testing.T) {
	dna := DNA("CGATTGGG")
	dna.Count('T')
	count1, err := dna.Count('T')
	if err != nil {
		t.Fatal(err)
	}
	count2, err := dna.Count('T')
	if err != nil {
		t.Fatal(err)
	}
	if count1 != count2 || count2 != 2 {
		t.Fatalf("Got %v, expected %v", []int{count1, count2}, []int{2, 2})
	}
}

type histogramTest struct {
	strand   DNA
	expected Histogram
	err      bool
}

var histogramTests = []histogramTest{
	{
		"",
		Histogram{'A': 0, 'C': 0, 'T': 0, 'G': 0},
		false,
	},
	{
		"GGGGGGGG",
		Histogram{'A': 0, 'C': 0, 'T': 0, 'G': 8},
		false,
	},
	{
		"AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC",
		Histogram{'A': 20, 'C': 12, 'T': 21, 'G': 17},
		false,
	},
	{
		"GGXXX",
		nil,
		true,
	},
}

func TestSequenceHistograms(t *testing.T) {
	for _, tt := range histogramTests {
		counts, err := tt.strand.Counts()
		if tt.err && err == nil {
			t.Fatalf("DNA{ %q }: expected error but didn't get one.", tt.strand)
		} else if !tt.err && err != nil {
			t.Fatalf("DNA{ %q }: expected no error but got error %s", tt.strand, err.Error())
		} else if !tt.err && !reflect.DeepEqual(counts, tt.expected) {
			t.Fatalf("DNA{ %q }: Got %v, expected %v", tt.strand, counts, tt.expected)
		}
	}
}

func BenchmarkSequenceHistograms(b *testing.B) {
	for _, tt := range histogramTests {
		for i := 0; i < b.N; i++ {
			tt.strand.Counts()
		}
	}
}

const targetTestVersion = 2

func TestTestVersion(t *testing.T) {
	if testVersion != targetTestVersion {
		t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
	}
}
<|endoftext|>


<commit_before>package resource

import(
	. "github.com/talbright/go-desk/types"
)

type CaseEvent struct {
	Type            *string                  `json:"type,omitempty"`
	Context         *string                  `json:"type,omitempty"`
	CreatedAt       *Timestamp               `json:"created_at,omitempty"`
	Changes         []map[string]interface{} `json:"changes,omitempty"`
	Resource
}

func NewCaseEvent() *CaseEvent {
	case_event := &CaseEvent{}
	return case_event
}

func (c CaseEvent) String() string {
	return Stringify(c)
}

<commit_msg>fix the CaseEvent type json metadata<commit_after>package resource

import(
	. "github.com/talbright/go-desk/types"
)

type CaseEvent struct {
	Type            *string                  `json:"type,omitempty"`
	Context         *string                  `json:"context,omitempty"`
	CreatedAt       *Timestamp               `json:"created_at,omitempty"`
	Changes         []map[string]interface{} `json:"changes,omitempty"`
	Resource
}

func NewCaseEvent() *CaseEvent {
	case_event := &CaseEvent{}
	return case_event
}

func (c CaseEvent) String() string {
	return Stringify(c)
}

<|endoftext|>


<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package cmd

import (
	"io"
	"strings"

	"github.com/juju/cmd"
	"github.com/juju/errors"

	"github.com/juju/juju/cmd/envcmd"
)

// UploadClient has the API client methods needed by UploadCommand.
type UploadClient interface {
	Upload(service, name string, resource io.Reader) error
	Close() error
}

// UploadDeps is a type that contains external functions that Upload depends on
// to function.
type UploadDeps struct {
	// NewClient returns the value that wraps the API for uploading to the server.
	NewClient func(*UploadCommand) (UploadClient, error)
	// OpenResource handles creating a reader from the resource path.
	OpenResource func(path string) (io.ReadCloser, error)
}

// UploadCommand implements the upload command.
type UploadCommand struct {
	deps UploadDeps
	envcmd.EnvCommandBase
	service       string
	resourceFiles []resourceFile
	resources     map[string]bool
}

// NewUploadCommand returns a new command that lists resources defined
// by a charm.
func NewUploadCommand(deps UploadDeps) *UploadCommand {
	return &UploadCommand{deps: deps}
}

// Info implements cmd.Command.Info
func (c *UploadCommand) Info() *cmd.Info {
	return &cmd.Info{
		Name:    "upload",
		Args:    "service name=file [name2=file2 ...]",
		Purpose: "upload a file as a resource for a service",
		Doc: `
This command uploads a file from your local disk to the juju controller to be
used as a resource for a service.
`,
	}
}

// Init implements cmd.Command.Init. It will return an error satisfying
// errors.BadRequest if you give it an incorrect number of arguments.
func (c *UploadCommand) Init(args []string) error {
	switch len(args) {
	case 0:
		return errors.BadRequestf("missing service name")
	case 1:
		return errors.BadRequestf("no resource specified")
	}
	c.service = args[0]

	c.resources = make(map[string]bool)

	for _, arg := range args[1:] {
		if err := c.addResourceFile(arg); err != nil {
			return errors.Trace(err)
		}
	}

	return nil
}

// addResourceFile parses the given arg into a name and a resource file,
// and saves it in c.resourceFiles.
func (c *UploadCommand) addResourceFile(arg string) error {
	rf, err := parseResourceFile(c.service, arg)
	if err != nil {
		return errors.Trace(err)
	}

	// TODO(ericsnow) Allow last one to win (a standard CLI approach)?
	if c.resources[rf.name] {
		// TODO(ericsnow) Use a better error message.
		return errors.AlreadyExistsf("resource %q", rf.name)
	}
	c.resourceFiles = append(c.resourceFiles, rf)
	c.resources[rf.name] = true
	return nil
}

// Run implements cmd.Command.Run.
func (c *UploadCommand) Run(*cmd.Context) error {
	apiclient, err := c.deps.NewClient(c)
	if err != nil {
		return errors.Annotatef(err, "can't connect to %s", c.ConnectionName())
	}
	defer apiclient.Close()

	errs := []error{}

	for _, rf := range c.resourceFiles {
		// don't want to do a bulk upload since we're doing potentially large
		// file uploads.
		if err := c.upload(rf.service, rf.name, rf.filename, apiclient); err != nil {
			name := rf.service + "/" + rf.name
			errs = append(errs, errors.Annotatef(err, "failed to upload resource %q", name))
		}
	}
	switch len(errs) {
	case 0:
		return nil
	case 1:
		return errs[0]
	default:
		msgs := make([]string, len(errs))
		for i := range errs {
			msgs[i] = errs[i].Error()
		}
		return errors.Errorf(strings.Join(msgs, "\n"))
	}
}

// upload opens the given file and calls the apiclient to upload it to the given
// service with the given name.
func (c *UploadCommand) upload(service, name, file string, client UploadClient) error {
	f, err := c.deps.OpenResource(file)
	if err != nil {
		return errors.Trace(err)
	}
	defer f.Close()
	err = client.Upload(service, name, f)
	return errors.Trace(err)
}
<commit_msg>Improve an error message.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package cmd

import (
	"fmt"
	"io"
	"strings"

	"github.com/juju/cmd"
	"github.com/juju/errors"

	"github.com/juju/juju/cmd/envcmd"
)

// UploadClient has the API client methods needed by UploadCommand.
type UploadClient interface {
	Upload(service, name string, resource io.Reader) error
	Close() error
}

// UploadDeps is a type that contains external functions that Upload depends on
// to function.
type UploadDeps struct {
	// NewClient returns the value that wraps the API for uploading to the server.
	NewClient func(*UploadCommand) (UploadClient, error)
	// OpenResource handles creating a reader from the resource path.
	OpenResource func(path string) (io.ReadCloser, error)
}

// UploadCommand implements the upload command.
type UploadCommand struct {
	deps UploadDeps
	envcmd.EnvCommandBase
	service       string
	resourceFiles []resourceFile
	resources     map[string]bool
}

// NewUploadCommand returns a new command that lists resources defined
// by a charm.
func NewUploadCommand(deps UploadDeps) *UploadCommand {
	return &UploadCommand{deps: deps}
}

// Info implements cmd.Command.Info
func (c *UploadCommand) Info() *cmd.Info {
	return &cmd.Info{
		Name:    "upload",
		Args:    "service name=file [name2=file2 ...]",
		Purpose: "upload a file as a resource for a service",
		Doc: `
This command uploads a file from your local disk to the juju controller to be
used as a resource for a service.
`,
	}
}

// Init implements cmd.Command.Init. It will return an error satisfying
// errors.BadRequest if you give it an incorrect number of arguments.
func (c *UploadCommand) Init(args []string) error {
	switch len(args) {
	case 0:
		return errors.BadRequestf("missing service name")
	case 1:
		return errors.BadRequestf("no resource specified")
	}
	c.service = args[0]

	c.resources = make(map[string]bool)

	for _, arg := range args[1:] {
		if err := c.addResourceFile(arg); err != nil {
			return errors.Trace(err)
		}
	}

	return nil
}

// addResourceFile parses the given arg into a name and a resource file,
// and saves it in c.resourceFiles.
func (c *UploadCommand) addResourceFile(arg string) error {
	rf, err := parseResourceFile(c.service, arg)
	if err != nil {
		return errors.Trace(err)
	}

	// TODO(ericsnow) Allow last one to win (a standard CLI approach)?
	if c.resources[rf.name] {
		msg := fmt.Sprintf("duplicate resource %q", rf.name)
		return errors.NewAlreadyExists(nil, msg)
	}
	c.resourceFiles = append(c.resourceFiles, rf)
	c.resources[rf.name] = true
	return nil
}

// Run implements cmd.Command.Run.
func (c *UploadCommand) Run(*cmd.Context) error {
	apiclient, err := c.deps.NewClient(c)
	if err != nil {
		return errors.Annotatef(err, "can't connect to %s", c.ConnectionName())
	}
	defer apiclient.Close()

	errs := []error{}

	for _, rf := range c.resourceFiles {
		// don't want to do a bulk upload since we're doing potentially large
		// file uploads.
		if err := c.upload(rf.service, rf.name, rf.filename, apiclient); err != nil {
			name := rf.service + "/" + rf.name
			errs = append(errs, errors.Annotatef(err, "failed to upload resource %q", name))
		}
	}
	switch len(errs) {
	case 0:
		return nil
	case 1:
		return errs[0]
	default:
		msgs := make([]string, len(errs))
		for i := range errs {
			msgs[i] = errs[i].Error()
		}
		return errors.Errorf(strings.Join(msgs, "\n"))
	}
}

// upload opens the given file and calls the apiclient to upload it to the given
// service with the given name.
func (c *UploadCommand) upload(service, name, file string, client UploadClient) error {
	f, err := c.deps.OpenResource(file)
	if err != nil {
		return errors.Trace(err)
	}
	defer f.Close()
	err = client.Upload(service, name, f)
	return errors.Trace(err)
}
<|endoftext|>


<commit_before>// call somatic variants with multiple tumor samples associated with 1 normal. This uses
// the genotype likelihood approach from bcbio and speedseq but allows for multiple tumor
// samples. Anything that meets the criteria will have a "PASS" or "." FILTER and a list
// of tumor samples with the somatic variant in INFO["SOMATIC"]
// Copied from bcbio and speedseq.
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/brentp/vcfgo"
	"github.com/brentp/xopen"
)

func getTumorLOD(gls []float64) float64 {
	lod := -1.0
	if len(gls) < 3 {
		return lod
	}
	for _, gl := range gls[1:] {
		t := gl - gls[0]
		if t > lod {
			lod = t
		}
	}
	return lod
}

func getNormalLOD(gls []float64, thresh float64) float64 {
	lod := 1e7
	if len(gls) < 3 {
		return thresh
	}
	for _, gl := range gls[1:] {
		t := gls[0] - gl
		if t < lod {
			lod = t
		}
	}
	return lod
}

func somaticLOD(normalGLs []float64, tumorGLs []float64, thresh float64) bool {
	normalLOD := getNormalLOD(normalGLs, thresh)
	tumorLOD := getTumorLOD(tumorGLs)
	return tumorLOD >= thresh && normalLOD >= thresh
}

func getFreq(refCount int, altCounts []int) float64 {
	ac := 0
	for _, a := range altCounts {
		ac += a
	}
	return float64(ac) / float64(refCount+ac)
}

func somaticFreqs(normal *vcfgo.SampleGenotype, tumor *vcfgo.SampleGenotype, freqRatio float64) bool {
	var nFreq, tFreq float64
	nrd, err := normal.RefDepth()
	if err != nil {
		nFreq = 0.0
	} else {
		nads, err := normal.AltDepths()
		if err != nil {
			nFreq = 0.0
		} else {
			nFreq = getFreq(nrd, nads)
		}
	}
	trd, err := tumor.RefDepth()
	if err != nil {
		tFreq = 0.0
	} else {
		tads, err := tumor.AltDepths()
		if err != nil {
			tFreq = 0.0
		} else {
			tFreq = getFreq(trd, tads)
		}
	}
	return nFreq <= 0.001 || nFreq <= tFreq/freqRatio

}

func Somatics(v *vcfgo.Variant, normalIdx int, thresh float64, freqRatio float64, skipMissing bool) []string {

	normal := v.Samples[normalIdx]
	somatics := make([]string, 0)
	for i, tumor := range v.Samples {
		if i == normalIdx {
			continue
		}
		// if we don't have GLs for normal, dont call a somatic.
		if skipMissing && len(normal.GL) == 0 {
			return somatics
		}
		if !somaticFreqs(normal, tumor, freqRatio) {
			continue
		}
		if somaticLOD(normal.GL, tumor.GL, thresh) {
			somatics = append(somatics, v.Header.SampleNames[i])
		}
	}
	return somatics
}

func check(e error) {
	if e != nil {
		log.Fatal(e)
	}
}

func sampleIndex(samples []string, sample string) int {
	for p, s := range samples {
		if s == sample {
			return p
		}
	}
	return -1
}

func main() {
	normalStr := flag.String("normal", "", "sample name of the normal sample. Default is the first sample")
	threshold := flag.Float64("threshold", 3.5, "threshold for difference in GLs to call a somatic variant")
	freqRatio := flag.Float64("freq-ratio", 2.7, "frequency in the tumor must be at least this many times the frequency in the normal")
	onlySomatic := flag.Bool("only-somatic", false, "print only the PASSing somatic variants (default is to set a flag and print all variants")
	skipMissing := flag.Bool("skip-missing-normals", false, "skip variants with missing normal. default is flag these as somatic.")
	flag.Parse()
	vcfs := flag.Args()
	if len(vcfs) != 1 {
		fmt.Printf("-----------------------------------------------------------------------------------\n")
		fmt.Printf("---------------- call somatic variants present in any tumor sample ----------------\n")
		fmt.Printf("----- uses the method from bcbio and speedseq but for multiple tumor samples ------\n")
		fmt.Printf("-----------------------------------------------------------------------------------\n")
		fmt.Printf("%s normal-and-tumors.vcf.gz\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	fhr, err := xopen.Ropen(vcfs[0])
	check(err)

	rdr, err := vcfgo.NewReader(fhr, false)
	check(err)

	normal := 0
	if *normalStr != "" {
		normal = sampleIndex(rdr.Header.SampleNames, *normalStr)
		if normal == -1 {
			log.Fatalf("sample %s: not found in vcf (%v)\n", *normalStr, rdr.Header.SampleNames)
		}
	}

	fhw, err := xopen.Wopen("-")
	check(err)

	hdr := rdr.Header
	hdr.Filters["NOT_SOMATIC"] = "not somatic between normal and any tumor"

	hdr.Infos["SOMATIC"] = &vcfgo.Info{Id: "SOMATIC", Number: "1", Type: "String", Description: "Tumor samples with somatic event"}

	wtr, err := vcfgo.NewWriter(fhw, hdr)
	check(err)

	log.Printf("using %s as the normal sample\n", hdr.SampleNames[normal])

	j := 0
	for v := rdr.Read(); v != nil; v = rdr.Read() {
		j++
		if *onlySomatic && !(v.Filter == "." || v.Filter == "PASS") {
			continue
		}
		somatics := Somatics(v, normal, *threshold, *freqRatio, *skipMissing)
		if len(somatics) > 0 {
			v.Info.Add("SOMATIC", strings.Join(somatics, "|"))
		} else {
			if *onlySomatic {
				continue
			}
			if v.Filter == "." || v.Filter == "PASS" {
				v.Filter = ""
			}
			if v.Filter != "" {
				v.Filter += ";"
			}
			v.Filter += "NOT_SOMATIC"
		}
		wtr.WriteVariant(v)
	}
	log.Println("VCF warnings:", rdr.Error())
	log.Printf("evaluated %d variants\n", j)
	fhw.Close()
}
<commit_msg>pull normal check out of loop.<commit_after>// call somatic variants with multiple tumor samples associated with 1 normal. This uses
// the genotype likelihood approach from bcbio and speedseq but allows for multiple tumor
// samples. Anything that meets the criteria will have a "PASS" or "." FILTER and a list
// of tumor samples with the somatic variant in INFO["SOMATIC"]
// Copied from bcbio and speedseq.
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/brentp/vcfgo"
	"github.com/brentp/xopen"
)

func getTumorLOD(gls []float64) float64 {
	lod := -1.0
	if len(gls) < 3 {
		return lod
	}
	for _, gl := range gls[1:] {
		t := gl - gls[0]
		if t > lod {
			lod = t
		}
	}
	return lod
}

func getNormalLOD(gls []float64, thresh float64) float64 {
	lod := 1e7
	if len(gls) < 3 {
		return thresh
	}
	for _, gl := range gls[1:] {
		t := gls[0] - gl
		if t < lod {
			lod = t
		}
	}
	return lod
}

func somaticLOD(normalGLs []float64, tumorGLs []float64, thresh float64) bool {
	normalLOD := getNormalLOD(normalGLs, thresh)
	tumorLOD := getTumorLOD(tumorGLs)
	return tumorLOD >= thresh && normalLOD >= thresh
}

func getFreq(refCount int, altCounts []int) float64 {
	ac := 0
	for _, a := range altCounts {
		ac += a
	}
	return float64(ac) / float64(refCount+ac)
}

func somaticFreqs(normal *vcfgo.SampleGenotype, tumor *vcfgo.SampleGenotype, freqRatio float64) bool {
	var nFreq, tFreq float64
	nrd, err := normal.RefDepth()
	if err != nil {
		nFreq = 0.0
	} else {
		nads, err := normal.AltDepths()
		if err != nil {
			nFreq = 0.0
		} else {
			nFreq = getFreq(nrd, nads)
		}
	}
	trd, err := tumor.RefDepth()
	if err != nil {
		tFreq = 0.0
	} else {
		tads, err := tumor.AltDepths()
		if err != nil {
			tFreq = 0.0
		} else {
			tFreq = getFreq(trd, tads)
		}
	}
	return nFreq <= 0.001 || nFreq <= tFreq/freqRatio

}

func Somatics(v *vcfgo.Variant, normalIdx int, thresh float64, freqRatio float64, skipMissing bool) []string {

	normal := v.Samples[normalIdx]
	somatics := make([]string, 0)
	// if we don't have GLs for normal, dont call a somatic.
	if skipMissing && len(normal.GL) == 0 {
		return somatics
	}
	for i, tumor := range v.Samples {
		if i == normalIdx {
			continue
		}
		if !somaticFreqs(normal, tumor, freqRatio) {
			continue
		}
		if somaticLOD(normal.GL, tumor.GL, thresh) {
			somatics = append(somatics, v.Header.SampleNames[i])
		}
	}
	return somatics
}

func check(e error) {
	if e != nil {
		log.Fatal(e)
	}
}

func sampleIndex(samples []string, sample string) int {
	for p, s := range samples {
		if s == sample {
			return p
		}
	}
	return -1
}

func main() {
	normalStr := flag.String("normal", "", "sample name of the normal sample. Default is the first sample")
	threshold := flag.Float64("threshold", 3.5, "threshold for difference in GLs to call a somatic variant")
	freqRatio := flag.Float64("freq-ratio", 2.7, "frequency in the tumor must be at least this many times the frequency in the normal")
	onlySomatic := flag.Bool("only-somatic", false, "print only the PASSing somatic variants (default is to set a flag and print all variants")
	skipMissing := flag.Bool("skip-missing-normals", false, "skip variants with missing normal. default is flag these as somatic.")
	flag.Parse()
	vcfs := flag.Args()
	if len(vcfs) != 1 {
		fmt.Printf("-----------------------------------------------------------------------------------\n")
		fmt.Printf("---------------- call somatic variants present in any tumor sample ----------------\n")
		fmt.Printf("----- uses the method from bcbio and speedseq but for multiple tumor samples ------\n")
		fmt.Printf("-----------------------------------------------------------------------------------\n")
		fmt.Printf("%s normal-and-tumors.vcf.gz\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	/*
		f, err := os.Create("cpu.pprof")
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	*/

	fhr, err := xopen.Ropen(vcfs[0])
	check(err)

	rdr, err := vcfgo.NewReader(fhr, false)
	check(err)

	normal := 0
	if *normalStr != "" {
		normal = sampleIndex(rdr.Header.SampleNames, *normalStr)
		if normal == -1 {
			log.Fatalf("sample %s: not found in vcf (%v)\n", *normalStr, rdr.Header.SampleNames)
		}
	}

	fhw, err := xopen.Wopen("-")
	check(err)

	hdr := rdr.Header
	hdr.Filters["NOT_SOMATIC"] = "not somatic between normal and any tumor"

	hdr.Infos["SOMATIC"] = &vcfgo.Info{Id: "SOMATIC", Number: "1", Type: "String", Description: "Tumor samples with somatic event"}

	wtr, err := vcfgo.NewWriter(fhw, hdr)
	check(err)

	log.Printf("using %s as the normal sample\n", hdr.SampleNames[normal])

	j := 0
	for v := rdr.Read(); v != nil; v = rdr.Read() {
		j++
		if *onlySomatic && !(v.Filter == "." || v.Filter == "PASS") {
			continue
		}
		somatics := Somatics(v, normal, *threshold, *freqRatio, *skipMissing)
		if len(somatics) > 0 {
			v.Info.Add("SOMATIC", strings.Join(somatics, "|"))
		} else {
			if *onlySomatic {
				continue
			}
			if v.Filter == "." || v.Filter == "PASS" {
				v.Filter = ""
			}
			if v.Filter != "" {
				v.Filter += ";"
			}
			v.Filter += "NOT_SOMATIC"
		}
		wtr.WriteVariant(v)
	}
	log.Println("VCF warnings:", rdr.Error())
	log.Printf("evaluated %d variants\n", j)
	fhw.Close()
}
<|endoftext|>


<commit_before>package ctxdownload_test

import (
	"fmt"
	"github.com/northbright/ctx/ctxdownload"
	"golang.org/x/net/context"
	"os"
	"time"
)

// Run "go test -c && ./ctxdownload.test"
func ExampleDownload() {
	// Download a zip file to test ctxdownload.Download().
	url := "https://github.com/northbright/plants/archive/master.zip"
	outDir := "./download"
	fileName := "" // If file name is empty, it will try to detect file name in response Header in Download().

	// Set total timeout. You'll get "deadline exceeded" error as expected.
	// To make download successful, set it to 300 or longer.
	totalTimeoutSeconds := 20
	totalTimeout := time.Duration(time.Duration(totalTimeoutSeconds) * time.Second)

	// Make a context to carry a deadline(timeout).
	// See http://blog.golang.org/context to for more information.
	ctx, cancel := context.WithTimeout(context.Background(), totalTimeout)
	defer cancel()

	// HTTP request timeout(NOT download timeout).
	requestTimeoutSeconds := 10

	// Make buffer.
	buf := make([]byte, 2*1024*1024)

	// Start download.
	// It will cancel download if cancel() is called in other goroutines.
	// It will stop download if deadline is exceeded(timeout).
	downloadedFileName, err := ctxdownload.Download(ctx, url, outDir, fileName, buf, requestTimeoutSeconds)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ctxdownload.Download() err: %v\n", err)
	} else {
		fmt.Fprintf(os.Stderr, "ctxdownload.Download() succeeded.\nDownloaded File: %v\n", downloadedFileName)
	}

	// Output:
}
<commit_msg>Import non-standard packages after standard ones.<commit_after>package ctxdownload_test

import (
	"fmt"
	"golang.org/x/net/context"
	"os"
	"time"

	"github.com/northbright/ctx/ctxdownload"
)

// Run "go test -c && ./ctxdownload.test"
func ExampleDownload() {
	// Download a zip file to test ctxdownload.Download().
	url := "https://github.com/northbright/plants/archive/master.zip"
	outDir := "./download"
	fileName := "" // If file name is empty, it will try to detect file name in response Header in Download().

	// Set total timeout. You'll get "deadline exceeded" error as expected.
	// To make download successful, set it to 300 or longer.
	totalTimeoutSeconds := 20
	totalTimeout := time.Duration(time.Duration(totalTimeoutSeconds) * time.Second)

	// Make a context to carry a deadline(timeout).
	// See http://blog.golang.org/context to for more information.
	ctx, cancel := context.WithTimeout(context.Background(), totalTimeout)
	defer cancel()

	// HTTP request timeout(NOT download timeout).
	requestTimeoutSeconds := 10

	// Make buffer.
	buf := make([]byte, 2*1024*1024)

	// Start download.
	// It will cancel download if cancel() is called in other goroutines.
	// It will stop download if deadline is exceeded(timeout).
	downloadedFileName, err := ctxdownload.Download(ctx, url, outDir, fileName, buf, requestTimeoutSeconds)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ctxdownload.Download() err: %v\n", err)
	} else {
		fmt.Fprintf(os.Stderr, "ctxdownload.Download() succeeded.\nDownloaded File: %v\n", downloadedFileName)
	}

	// Output:
}
<|endoftext|>


<commit_before>package torrent

import (
	"time"
)

// Implements heap functions such that [0] is the worst connection.
type worstConns struct {
	c []*connection
	t *torrent
}

func (me worstConns) Len() int      { return len(me.c) }
func (me worstConns) Swap(i, j int) { me.c[i], me.c[j] = me.c[j], me.c[i] }

func (me *worstConns) Pop() (ret interface{}) {
	old := me.c
	n := len(old)
	ret = old[n-1]
	me.c = old[:n-1]
	return
}

func (me *worstConns) Push(x interface{}) {
	me.c = append(me.c, x.(*connection))
}

type worstConnsSortKey struct {
	level int
	age   time.Duration
}

func (me worstConnsSortKey) Less(other worstConnsSortKey) bool {
	if me.level != other.level {
		return me.level > other.level
	}
	return me.age > other.age
}

func (me worstConns) key(i int) (key worstConnsSortKey) {
	c := me.c[i]
	if time.Now().Sub(c.completedHandshake) >= 30*time.Second && !me.t.connHasWantedPieces(c) {
		key.level = 1
	}
	key.age = time.Duration(1+3*c.UnwantedChunksReceived) * time.Now().Sub(func() time.Time {
		if !c.lastUsefulChunkReceived.IsZero() {
			return c.lastUsefulChunkReceived
		}
		return c.completedHandshake.Add(-time.Minute)
	}()) / time.Duration(1+c.UsefulChunksReceived)
	return
}

func (me worstConns) Less(i, j int) bool {
	return me.key(i).Less(me.key(j))
}
<commit_msg>When metainfo isn't complete, treat peers that don't support ut_metadata as useless<commit_after>package torrent

import (
	"time"
)

// Implements heap functions such that [0] is the worst connection.
type worstConns struct {
	c []*connection
	t *torrent
}

func (me worstConns) Len() int      { return len(me.c) }
func (me worstConns) Swap(i, j int) { me.c[i], me.c[j] = me.c[j], me.c[i] }

func (me *worstConns) Pop() (ret interface{}) {
	old := me.c
	n := len(old)
	ret = old[n-1]
	me.c = old[:n-1]
	return
}

func (me *worstConns) Push(x interface{}) {
	me.c = append(me.c, x.(*connection))
}

type worstConnsSortKey struct {
	// Peer has something we want.
	useless bool
	// A fabricated duration since peer was last helpful.
	age time.Duration
}

func (me worstConnsSortKey) Less(other worstConnsSortKey) bool {
	if me.useless != other.useless {
		return me.useless
	}
	return me.age > other.age
}

func (me worstConns) key(i int) (key worstConnsSortKey) {
	c := me.c[i]
	// Peer has had time to declare what they have.
	if time.Now().Sub(c.completedHandshake) >= 30*time.Second {
		if !me.t.haveInfo() {
			if _, ok := c.PeerExtensionIDs["ut_metadata"]; !ok {
				key.useless = true
			}
		} else {
			if !me.t.connHasWantedPieces(c) {
				key.useless = true
			}
		}
	}
	key.age = time.Duration(1+3*c.UnwantedChunksReceived) * time.Now().Sub(func() time.Time {
		if !c.lastUsefulChunkReceived.IsZero() {
			return c.lastUsefulChunkReceived
		}
		return c.completedHandshake.Add(-time.Minute)
	}()) / time.Duration(1+c.UsefulChunksReceived)
	return
}

func (me worstConns) Less(i, j int) bool {
	return me.key(i).Less(me.key(j))
}
<|endoftext|>


<commit_before>// Copyright 2018 The ACH Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.

package ach

import (
	"bytes"
	"strings"
	"testing"
)

// testPPDWrite writes a PPD ACH file
func testPPDWrite(t testing.TB) {
	file := NewFile().SetHeader(mockFileHeader())
	entry := mockEntryDetail()
	entry.AddAddenda(mockAddenda05())
	batch := NewBatchPPD(mockBatchPPDHeader())
	batch.SetHeader(mockBatchHeader())
	batch.AddEntry(entry)
	batch.Create()
	file.AddBatch(batch)

	if err := file.Create(); err != nil {
		t.Errorf("%T: %s", err, err)
	}
	if err := file.Validate(); err != nil {
		t.Errorf("%T: %s", err, err)
	}

	b := &bytes.Buffer{}
	f := NewWriter(b)

	if err := f.WriteAll([]*File{file}); err != nil {
		t.Errorf("%T: %s", err, err)
	}
	r := NewReader(strings.NewReader(b.String()))
	_, err := r.Read()
	if err != nil {
		t.Errorf("%T: %s", err, err)
	}
	if err = r.File.Validate(); err != nil {
		t.Errorf("%T: %s", err, err)
	}
}

// TestPPDWrite tests writing a PPD ACH file
func TestPPDWrite(t *testing.T) {
	testPPDWrite(t)
}

// BenchmarkPPDWrite benchmarks writing a PPD ACH file
func BenchmarkPPDWrite(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		testPPDWrite(b)
	}
}
<commit_msg># 207 test coverage writer_test.go<commit_after>// Copyright 2018 The ACH Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.

package ach

import (
	"bytes"
	"strings"
	"testing"
)

// testPPDWrite writes a PPD ACH file
func testPPDWrite(t testing.TB) {
	file := NewFile().SetHeader(mockFileHeader())
	entry := mockEntryDetail()
	entry.AddAddenda(mockAddenda05())
	batch := NewBatchPPD(mockBatchPPDHeader())
	batch.SetHeader(mockBatchHeader())
	batch.AddEntry(entry)
	batch.Create()
	file.AddBatch(batch)

	if err := file.Create(); err != nil {
		t.Errorf("%T: %s", err, err)
	}
	if err := file.Validate(); err != nil {
		t.Errorf("%T: %s", err, err)
	}

	b := &bytes.Buffer{}
	f := NewWriter(b)

	if err := f.WriteAll([]*File{file}); err != nil {
		t.Errorf("%T: %s", err, err)
	}
	r := NewReader(strings.NewReader(b.String()))
	_, err := r.Read()
	if err != nil {
		t.Errorf("%T: %s", err, err)
	}
	if err = r.File.Validate(); err != nil {
		t.Errorf("%T: %s", err, err)
	}
}

// TestPPDWrite tests writing a PPD ACH file
func TestPPDWrite(t *testing.T) {
	testPPDWrite(t)
}

// BenchmarkPPDWrite benchmarks validating writing a PPD ACH file
func BenchmarkPPDWrite(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		testPPDWrite(b)
	}
}

// testFileWriteErr validates error for file write
func testFileWriteErr(t testing.TB) {
	file := NewFile().SetHeader(mockFileHeader())
	entry := mockEntryDetail()
	entry.AddAddenda(mockAddenda05())
	batch := NewBatchPPD(mockBatchPPDHeader())
	batch.SetHeader(mockBatchHeader())
	batch.AddEntry(entry)
	batch.Create()
	file.AddBatch(batch)

	if err := file.Create(); err != nil {
		t.Errorf("%T: %s", err, err)
	}
	if err := file.Validate(); err != nil {
		t.Errorf("%T: %s", err, err)
	}

	file.Batches[0].GetControl().EntryAddendaCount = 10

	b := &bytes.Buffer{}
	f := NewWriter(b)

	if err := f.WriteAll([]*File{file}); err != nil {
		if e, ok := err.(*FileError); ok {
			if e.FieldName != "EntryAddendaCount" {
				t.Errorf("%T: %s", err, err)
			}
		} else {
			t.Errorf("%T: %s", err, err)
		}
	}
}

// TestFileWriteErr tests validating error for file write
func TestFileWriteErr(t *testing.T) {
	testFileWriteErr(t)
}

// BenchmarkFileWriteErr benchmarks error for file write
func BenchmarkFileWriteErr(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		testFileWriteErr(b)
	}
}
<|endoftext|>