go-hep.org/x/hep@v0.38.1/cmd/root2arrow/main_test.go (about)

     1  // Copyright ©2019 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main // import "go-hep.org/x/hep/cmd/root2arrow"
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"os/exec"
    11  	"testing"
    12  
    13  	"go-hep.org/x/hep/internal/diff"
    14  )
    15  
    16  func init() {
    17  	_, err := exec.LookPath("arrow-cat")
    18  	if err == nil {
    19  		return
    20  	}
    21  
    22  	o, err := exec.Command("go", "install", "git.sr.ht/~sbinet/go-arrow/ipc/cmd/arrow-cat@latest").CombinedOutput()
    23  	if err != nil {
    24  		panic(fmt.Errorf("could not install arrow-cat command:\n%v\nerr: %w", string(o), err))
    25  	}
    26  
    27  }
    28  
    29  func TestFile(t *testing.T) {
    30  	for _, tc := range []struct {
    31  		file   string
    32  		tree   string
    33  		stream bool
    34  		want   string
    35  	}{
    36  		{
    37  			file: "../../groot/testdata/simple.root",
    38  			tree: "tree",
    39  			want: "testdata/simple.root.file",
    40  		},
    41  		{
    42  			file:   "../../groot/testdata/simple.root",
    43  			tree:   "tree",
    44  			stream: true,
    45  			want:   "testdata/simple.root.stream",
    46  		},
    47  		{
    48  			file: "../../groot/testdata/leaves.root",
    49  			tree: "tree",
    50  			want: "testdata/leaves.root.file",
    51  		},
    52  		{
    53  			file:   "../../groot/testdata/leaves.root",
    54  			tree:   "tree",
    55  			stream: true,
    56  			want:   "testdata/leaves.root.stream",
    57  		},
    58  		{
    59  			file: "../../groot/testdata/embedded-std-vector.root",
    60  			tree: "modules",
    61  			want: "testdata/embedded-std-vector.root.file",
    62  		},
    63  		{
    64  			file:   "../../groot/testdata/embedded-std-vector.root",
    65  			tree:   "modules",
    66  			stream: true,
    67  			want:   "testdata/embedded-std-vector.root.stream",
    68  		},
    69  	} {
    70  		t.Run(tc.want, func(t *testing.T) {
    71  			f, err := os.CreateTemp("", "root2arrow-")
    72  			if err != nil {
    73  				t.Fatal(err)
    74  			}
    75  			f.Close()
    76  			defer os.Remove(f.Name())
    77  
    78  			err = process(f.Name(), tc.file, tc.tree, tc.stream)
    79  			if err != nil {
    80  				t.Fatal(err)
    81  			}
    82  
    83  			want, err := os.ReadFile(tc.want)
    84  			if err != nil {
    85  				t.Fatal(err)
    86  			}
    87  
    88  			got, err := arrowCat(f.Name())
    89  			if err != nil {
    90  				t.Fatal(err)
    91  			}
    92  
    93  			if got, want := string(got), string(want); got != want {
    94  				t.Fatalf(
    95  					"arrow file/stream differ:\n%s",
    96  					diff.Format(got, want),
    97  				)
    98  			}
    99  		})
   100  	}
   101  
   102  }
   103  
   104  func arrowCat(fname string) ([]byte, error) {
   105  	return exec.Command("arrow-cat", fname).CombinedOutput()
   106  }