// Tests that packages generated by the binder can be successfully compiled and // the requested tester run against it. func TestBindings(t *testing.T) { // Skip the test if no Go command can be found gocmd := runtime.GOROOT() + "/bin/go" if !common.FileExist(gocmd) { t.Skip("go sdk not found for testing") } // Skip the test if the go-ethereum sources are symlinked (https://github.com/golang/go/issues/14845) linkTestCode := fmt.Sprintf("package linktest\nfunc CheckSymlinks(){\nfmt.Println(backends.NewNilBackend())\n}") linkTestDeps, err := imports.Process("", []byte(linkTestCode), nil) if err != nil { t.Fatalf("failed check for goimports symlink bug: %v", err) } if !strings.Contains(string(linkTestDeps), "go-ethereum") { t.Skip("symlinked environment doesn't support bind (https://github.com/golang/go/issues/14845)") } // Create a temporary workspace for the test suite ws, err := ioutil.TempDir("", "") if err != nil { t.Fatalf("failed to create temporary workspace: %v", err) } defer os.RemoveAll(ws) pkg := filepath.Join(ws, "bindtest") if err = os.MkdirAll(pkg, 0700); err != nil { t.Fatalf("failed to create package: %v", err) } // Generate the test suite for all the contracts for i, tt := range bindTests { // Generate the binding and create a Go source file in the workspace bind, err := Bind([]string{tt.name}, []string{tt.abi}, []string{tt.bytecode}, "bindtest") if err != nil { t.Fatalf("test %d: failed to generate binding: %v", i, err) } if err = ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+".go"), []byte(bind), 0600); err != nil { t.Fatalf("test %d: failed to write binding: %v", i, err) } // Generate the test file with the injected test code code := fmt.Sprintf("package bindtest\nimport \"testing\"\nfunc Test%s(t *testing.T){\n%s\n}", tt.name, tt.tester) blob, err := imports.Process("", []byte(code), nil) if err != nil { t.Fatalf("test %d: failed to generate tests: %v", i, err) } if err := ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+"_test.go"), blob, 0600); err != nil { t.Fatalf("test %d: failed to write tests: %v", i, err) } } // Test the entire package and report any failures cmd := exec.Command(gocmd, "test", "-v") cmd.Dir = pkg if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("failed to run binding test: %v\n%s", err, out) } }
func goimports(src []byte) (string, string) { out, err := imports.Process("", src, nil) if err != nil { return "", fmt.Sprintf("%v", err) } return string(out), "" }
func TranspileFile(goFilename, phpFilename, phpStr string, gosrc io.Writer) error { parser := parser.NewParser() file, err := parser.Parse(phpFilename, phpStr) if err != nil { return fmt.Errorf("found errors while parsing %s: %s", phpFilename, err) } tg := Togo{currentScope: parser.FileSet.Scope} nodes := []goast.Node{} for _, node := range tg.beginScope(tg.currentScope) { nodes = append(nodes, node) } for _, phpNode := range file.Nodes { nodes = append(nodes, tg.ToGoStmt(phpNode.(phpast.Statement))) } buf := &bytes.Buffer{} if err = format.Node(buf, token.NewFileSet(), File(phpFilename[:len(phpFilename)-4], nodes...)); err != nil { return fmt.Errorf("error while formatting %s: %s", phpFilename, err) } imported, err := imports.Process(goFilename, buf.Bytes(), &imports.Options{AllErrors: true, Comments: true, TabIndent: true, TabWidth: 8}) if err != nil { return fmt.Errorf("error while getting imports for %s: %s", phpFilename, err) } _, err = gosrc.Write(imported) return err }
func fmtHandler(w http.ResponseWriter, r *http.Request) { var ( in = []byte(r.FormValue("body")) out []byte err error ) if len(in) > maxSnippetSize { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintln(w, "Compile server error.") return } if r.FormValue("imports") != "" { out, err = imports.Process("prog.go", in, nil) } else { out, err = format.Source(in) } var resp fmtResponse if err != nil { resp.Error = err.Error() } else { resp.Body = string(out) } w.Header().Set("Access-Control-Allow-Methods", "POST") w.Header().Set("Access-Control-Allow-Origin", *flagAllowOriginHeader) json.NewEncoder(w).Encode(resp) }
func validFormat(repoPath string, prFiles []*octokat.PullRequestFile) (formatted bool, files []string, err error) { repo, err := gitreader.OpenRepo(repoPath) if err != nil { return false, files, err } for _, file := range prFiles { name := file.FileName if strings.HasSuffix(name, ".go") && !strings.HasPrefix(name, "vendor/") { blob, err := repo.CatFile(file.Sha, name) if err != nil { return false, files, err } src, err := blob.Bytes() if err != nil { return false, files, err } res, err := imports.Process(name, src, nil) if err != nil { return false, files, err } if !bytes.Equal(src, res) { files = append(files, name) formatted = false } } } return len(files) == 0, files, err }
func convertFile(dest_path, source_path string, fixImports bool, types ...string) error { buf, err := genlib.Generate(source_path, types...) if err != nil { return err } if fixImports { buf, err = imports.Process(path.Base(source_path), buf, &imports.Options{ TabWidth: 8, TabIndent: true, Comments: true, Fragment: true, AllErrors: false, }) if err != nil { return err } } f, err := os.Create(dest_path) if err != nil { return err } _, err = io.Copy(f, bytes.NewBuffer(buf)) if err != nil { return err } return nil }
func assertOutput(inPath, lang, goldenPath string, t *testing.T) { assert := assert.New(t) emptyDS := datas.NewDataStore(chunks.NewMemoryStore()) // Will be DataStore containing imports depsDir, err := ioutil.TempDir("", "") assert.NoError(err) defer os.RemoveAll(depsDir) inFile, err := os.Open(inPath) assert.NoError(err) defer inFile.Close() goldenFile, err := os.Open(goldenPath) assert.NoError(err) defer goldenFile.Close() goldenBytes, err := ioutil.ReadAll(goldenFile) d.Chk.NoError(err) var buf bytes.Buffer pkg := pkg.ParseNomDL("gen", inFile, filepath.Dir(inPath), emptyDS) written := map[string]bool{} gen := newCodeGen(&buf, getBareFileName(inPath), lang, written, depsMap{}, pkg) gen.WritePackage() bs := buf.Bytes() if lang == "go" { bs, err = imports.Process("", bs, nil) d.Chk.NoError(err) } assert.Equal(string(goldenBytes), string(bs), "%s did not generate the same string", inPath) }
// FindInterface returns the import path and identifier of an interface. // For example, given "http.ResponseWriter", FindInterface returns // "net/http", "ResponseWriter". func FindInterface(iface string) (path string, pkg string, id string, err error) { if len(strings.Fields(iface)) != 1 { return "", "", "", fmt.Errorf("couldn't parse interface: %s", iface) } // Let goimports do the heavy lifting. src := []byte("package hack\n" + "var i " + iface) imp, err := imports.Process("", src, nil) if err != nil { return "", "", "", fmt.Errorf("couldn't parse interface: %s", iface) } // imp should now contain an appropriate import. // Parse out the import and the identifier. fset := token.NewFileSet() f, err := parser.ParseFile(fset, "", imp, 0) if err != nil { panic(err) } if len(f.Imports) == 0 { return "", "", "", fmt.Errorf("unrecognized interface: %s", iface) } raw := f.Imports[0].Path.Value // "io" path, err = strconv.Unquote(raw) // io if err != nil { panic(err) } decl := f.Decls[1].(*ast.GenDecl) // var i io.Reader spec := decl.Specs[0].(*ast.ValueSpec) // i io.Reader sel := spec.Type.(*ast.SelectorExpr) // io.Reader pkg = sel.X.(*ast.Ident).Name // io id = sel.Sel.Name // Reader return path, pkg, id, nil }
func format(file string, data []byte) error { out, err := imports.Process(file, data, nil) if err != nil { return err } return ioutil.WriteFile(file, out, 0600) }
func main() { flag.Usage = usage flag.Parse() if flag.NArg() < 2 { usage() } args := flag.Args() n := len(args) extras := args[:n-2] a := args[n-2:] inter, typeName := a[0], a[1] opts := GenOpts{Inter: inter, NoGoImports: !*goimports, NoNamedReturnValues: !*named, Extra: extras} if !*existing { pi, err := parse(typeName) check(err) opts.ImplName = pi.ptr + pi.name opts.PkgName = pi.pkg } else { opts.Existing = typeName if !strings.HasSuffix(typeName, "}") && !strings.HasSuffix(typeName, ")") { // We need to create a instance of this type. // Let's it's a struct/slice. opts.Existing += "{}" } } buf := new(bytes.Buffer) check(tm.Execute(buf, opts)) src, err := imports.Process("", buf.Bytes(), nil) check(err, "imports:", buf.String()) check(run(src), "run:", string(src)) }
func genTypesOnlyAPI(file string) error { apiGen := &api.API{ NoRemoveUnusedShapes: true, NoRenameToplevelShapes: true, } apiGen.Attach(file) apiGen.Setup() // to reset imports so that timestamp has an entry in the map. apiGen.APIGoCode() var buf bytes.Buffer err := typesOnlyTplAPI.Execute(&buf, apiGen) if err != nil { panic(err) } code := strings.TrimSpace(buf.String()) code = util.GoFmt(code) // Ignore dir error, filepath will catch it for an invalid path. os.Mkdir(apiGen.PackageName(), 0755) // Fix imports. codeWithImports, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", apiGen.PackageName(), code)), nil) if err != nil { fmt.Println(err) return err } outFile := filepath.Join(apiGen.PackageName(), "api.go") err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, codeWithImports)), 0644) if err != nil { return err } return nil }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) flag.Parse() var funcOut io.Writer if *functionDest == "-" { funcOut = os.Stdout } else { file, err := os.Create(*functionDest) if err != nil { glog.Fatalf("Couldn't open %v: %v", *functionDest, err) } defer file.Close() funcOut = file } data := new(bytes.Buffer) group, version := path.Split(*groupVersion) group = strings.TrimRight(group, "/") _, err := data.WriteString(fmt.Sprintf("package %v\n", version)) if err != nil { glog.Fatalf("error writing package line: %v", err) } versionPath := path.Join(pkgBase, group, version) generator := pkg_runtime.NewConversionGenerator(api.Scheme.Raw(), versionPath) apiShort := generator.AddImport(path.Join(pkgBase, "api")) generator.AddImport(path.Join(pkgBase, "api/resource")) // TODO(wojtek-t): Change the overwrites to a flag. generator.OverwritePackage(version, "") for _, knownType := range api.Scheme.KnownTypes(version) { if !strings.HasPrefix(knownType.PkgPath(), versionPath) { continue } if err := generator.GenerateConversionsForType(version, knownType); err != nil { glog.Errorf("error while generating conversion functions for %v: %v", knownType, err) } } generator.RepackImports(util.NewStringSet()) if err := generator.WriteImports(data); err != nil { glog.Fatalf("error while writing imports: %v", err) } if err := generator.WriteConversionFunctions(data); err != nil { glog.Fatalf("Error while writing conversion functions: %v", err) } if err := generator.RegisterConversionFunctions(data, fmt.Sprintf("%s.Scheme", apiShort)); err != nil { glog.Fatalf("Error while writing conversion functions: %v", err) } b, err := imports.Process("", data.Bytes(), nil) if err != nil { glog.Fatalf("error while update imports: %v", err) } if _, err := funcOut.Write(b); err != nil { glog.Fatalf("error while writing out the resulting file: %v", err) } }
// WriteFormattedFile formats the code with goimports and writes the result to // the given file, if file doesnt exists, it creates it func WriteFormattedFile(fileName string, model []byte) error { dest, err := imports.Process("", model, nil) if err != nil { return err } return Write(fileName, dest) }
func (gf *GoFile) Generate() ([]byte, error) { var buf bytes.Buffer if err := printer.Fprint(&buf, gf.FSet, gf.AstFile); err != nil { return nil, err } return imports.Process(gf.FileName, buf.Bytes(), nil) }
func main() { iface := "http.Handler" src := "package hack; var i " + iface // HL fmt.Println(src, "\n---") imp, _ := imports.Process("", []byte(src), nil) // HL // ignoring errors throughout this presentation fmt.Println(string(imp)) }
func formatGoFile(ffn string, content []byte) ([]byte, error) { opts := new(imports.Options) opts.TabIndent = true opts.TabWidth = 2 opts.Fragment = true opts.Comments = true return imports.Process(ffn, content, opts) }
// WriteAll writes the generated code for all Types and TypeWriters in the App to respective files. func (a *App) WriteAll() ([]string, error) { var written []string // one buffer for each file, keyed by file name buffers := make(map[string]*bytes.Buffer) // write the generated code for each Type & TypeWriter into memory for _, p := range a.Packages { for _, t := range p.Types { for _, tw := range a.TypeWriters { var b bytes.Buffer n, err := write(&b, a, p, t, tw) if err != nil { return written, err } // don't generate a file if no bytes were written by WriteHeader or WriteBody if n == 0 { continue } // append _test to file name if the source type is in a _test.go file f := strings.ToLower(fmt.Sprintf("%s_%s%s.go", t.Name, tw.Name(), t.test)) buffers[f] = &b } } } // validate generated ast's before committing to files for f, b := range buffers { if _, err := parser.ParseFile(token.NewFileSet(), f, b.String(), 0); err != nil { // TODO: prompt to write (ignored) _file on error? parsing errors are meaningless without. return written, err } } // format, remove unused imports, and commit to files for f, b := range buffers { src, err := imports.Process(f, b.Bytes(), nil) // shouldn't be an error if the ast parsing above succeeded if err != nil { return written, err } if err := writeFile(f, src); err != nil { return written, err } written = append(written, f) } return written, nil }
func WriteTo(w io.Writer, b *bytes.Buffer) error { opt := &imports.Options{Comments: true} res, err := imports.Process("mock.go", b.Bytes(), opt) if err != nil { return err } _, err = w.Write(res) return err }
func (g *Generator) Write(w io.Writer) error { opt := &imports.Options{Comments: true} res, err := imports.Process("mock.go", g.buf.Bytes(), opt) if err != nil { return err } w.Write(res) return nil }
func prettyfy(input []byte, wr io.Writer) error { output, err := imports.Process("storable.go", input, nil) if err != nil { printDocumentWithNumbers(string(input)) return err } _, err = wr.Write(output) return err }
func (gen CodeGenerator) GenerateFake() (string, error) { buf := new(bytes.Buffer) err := format.Node(buf, token.NewFileSet(), gen.sourceFile()) if err != nil { return "", err } code, err := imports.Process("", buf.Bytes(), nil) return commentLine() + prettifyCode(string(code)), err }
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error { opt := options if stdin { nopt := *options nopt.Fragment = true opt = &nopt } if in == nil { f, err := os.Open(filename) if err != nil { return err } defer f.Close() in = f } src, err := ioutil.ReadAll(in) if err != nil { return err } res, err := imports.Process(filename, src, opt) if err != nil { return err } if !bytes.Equal(src, res) { // formatting has changed if *list { fmt.Fprintln(out, filename) } if *write { err = ioutil.WriteFile(filename, res, 0) if err != nil { return err } } if *doDiff { data, err := diff(src, res) if err != nil { return fmt.Errorf("computing diff: %s", err) } fmt.Printf("diff %s gofmt/%s\n", filename, filename) out.Write(data) } } if !*list && !*write && !*doDiff { _, err = out.Write(res) } return err }
func genFull(file string) error { // Heavily inspired by https://github.com/aws/aws-sdk-go/blob/45ba2f817fbf625fe48cf9b51fae13e5dfba6171/internal/model/cli/gen-api/main.go#L36 // That code is copyright Amazon api := &api.API{} api.Attach(file) paginatorsFile := strings.Replace(file, "api-2.json", "paginators-1.json", -1) if _, err := os.Stat(paginatorsFile); err == nil { api.AttachPaginators(paginatorsFile) } docsFile := strings.Replace(file, "api-2.json", "docs-2.json", -1) if _, err := os.Stat(docsFile); err == nil { api.AttachDocs(docsFile) } api.Setup() processedApiCode, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", api.PackageName(), api.APIGoCode())), nil) if err != nil { fmt.Println(err) return err } // Ignore dir error, filepath will catch it for an invalid path. os.Mkdir(api.PackageName(), 0755) outFile := filepath.Join(api.PackageName(), "api.go") err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, processedApiCode)), 0644) if err != nil { return err } processedServiceCode, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", api.PackageName(), api.ServiceGoCode())), nil) if err != nil { fmt.Println(err) return err } outFile = filepath.Join(api.PackageName(), "service.go") err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, processedServiceCode)), 0644) if err != nil { return err } return nil }
func (s *Sandbox) imports(w http.ResponseWriter, r *http.Request) { code, _ := ioutil.ReadAll(r.Body) newCode, err := imports.Process("main.go", code, s.importsOpts) s.stats.Imports++ if err != nil { w.WriteHeader(400) w.Write([]byte(err.Error())) return } w.WriteHeader(200) w.Write(newCode) }
func main() { if len(os.Args) != 2 { panic("Must have exactly one argument; the output file") } type simpleTestMetadata struct { Name string Description string TaskDefinition string Timeout string ExitCodes map[string]int Tags []string Version string } _, filename, _, _ := runtime.Caller(0) metadataFiles, err := filepath.Glob(filepath.Join(path.Dir(filename), "..", "testdata", "simpletests", "*.json")) if err != nil || len(metadataFiles) == 0 { panic("No tests found" + err.Error()) } testMetadatas := make([]simpleTestMetadata, len(metadataFiles)) for i, f := range metadataFiles { data, err := ioutil.ReadFile(f) if err != nil { panic("Cannot read file " + f) } err = json.Unmarshal(data, &testMetadatas[i]) if err != nil { panic("Cannot parse " + f + ": " + err.Error()) } } simpleTests := template.Must(template.New("simpleTest").Parse(simpleTestPattern)) output := bytes.NewBuffer([]byte{}) err = simpleTests.Execute(output, testMetadatas) if err != nil { panic(err) } formattedOutput, err := imports.Process("", output.Bytes(), nil) if err != nil { fmt.Println(string(output.Bytes())) panic(err) } // Add '.go' so the arg can be used with 'go run' as well, without being interpreted as a file to run outputFile, err := os.Create(os.Args[1] + ".go") if err != nil { panic(err) } outputFile.Write(formattedOutput) }
func writeWithFormat(file, template string, binCode binCode) ([]byte, error) { var b bytes.Buffer w := bufio.NewWriter(&b) write(w, template, binCode) w.Flush() formatted, err := imports.Process(file, b.Bytes(), nil) if err != nil { return nil, err } return formatted, nil }
// Source returns source code in Go. func (t *Table) Source() (string, error) { b, err := render(t.SourceTemplate, t.parent.SourceTemplate, sourceTemplate, t) if err != nil { return "", err } b2, err := imports.Process("", b, nil) if err != nil { log.Println("WARN:", err) return string(b), nil } return string(b2), nil }
//Generate find all gtm templates in directory, generate go code and save it func Generate(dir string) (gtms []string, err error) { //get all files. with extension .gtm in dir gtms, err = filepath.Glob(path.Join(dir, "*"+*extension)) if err != nil { return } if *verbose { fmt.Println("find", len(gtms), "template files") } //generate it for _, gtm := range gtms { if *verbose { fmt.Println(gtm) } var filedata []byte //parse gtm filedata, err = Parse(gtm) if err != nil { return } if *verbose { scanner := bufio.NewScanner(bytes.NewReader(filedata)) var i int for scanner.Scan() { i++ fmt.Println(i, scanner.Text()) } // fmt.Println(string(filedata)) } // save filename := regexp.MustCompile(*extension+"$").ReplaceAllString(gtm, ".go") filedata, err = imports.Process(filename, filedata, nil) if err != nil { return } if err = ioutil.WriteFile(filename, filedata, 0755); err != nil { return } } return }
func main() { var out *os.File var err error flag.Parse() if len(flag.Args()) != 2 { log.Fatalln("invalid arguments") } pkgName := flag.Arg(0) chType := flag.Arg(1) if *outfile == "" { out = os.Stdout } else { out, err = os.Create(*outfile) if err != nil { log.Fatal(err) } } if chType == "" { log.Fatal("type not specified") } info := struct { PkgName string ChType string PrintName string }{ pkgName, chType, printableName(chType), } buf := &bytes.Buffer{} if err := tmpl.Execute(buf, info); err != nil { log.Fatal(err) } b, err := imports.Process(*outfile, buf.Bytes(), nil) if err != nil { log.Fatal(err) } buf = bytes.NewBuffer(b) if _, err := io.Copy(out, buf); err != nil { log.Fatal(err) } }
func renderCode(fullpath string, tmpl *template.Template, context interface{}) (fb []byte, err error) { targetBuf := bytes.Buffer{} err = tmpl.Execute(&targetBuf, context) if err != nil { return nil, fmt.Errorf("Problem rendering %s : %v", tmpl.Name(), err) } fb, err = imports.Process(fullpath, targetBuf.Bytes(), nil) if err != nil { return targetBuf.Bytes(), fmt.Errorf("Problem formatting %s : %v\n%s", tmpl.Name(), err, targetBuf.Bytes()) } return fb, err }