github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/config/paths_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     5  // Use of this source code is governed by a license that can
     6  // be found in the LICENSE file.
     7  
     8  package config
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"os/user"
    14  	"path/filepath"
    15  	"runtime"
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
    20  )
    21  
    22  type PathTest struct {
    23  	group    string
    24  	xdg      string
    25  	os       string
    26  	chain    string
    27  	part     string
    28  	expected string
    29  	disabled bool
    30  }
    31  
    32  func Test_GetPathTo(t *testing.T) {
    33  	userOs := runtime.GOOS
    34  	for index, test := range testSet1 {
    35  		if test.disabled || userOs != test.os {
    36  			continue
    37  		}
    38  
    39  		user, _ := user.Current()
    40  		testPath := ""
    41  		withChain := true
    42  		// TODO: bad xdg paths cause panic, so we turn this off
    43  		test.xdg = ""
    44  
    45  		if test.group == "Config" {
    46  			os.Setenv("XDG_CONFIG_HOME", test.xdg)
    47  			os.Setenv("TEST_OS", test.os)
    48  			globals := []string{"trueBlocks.toml", "makeClass.toml", "testRunner.toml", "abis/"}
    49  			for _, g := range globals {
    50  				if strings.HasPrefix(test.part, g) {
    51  					withChain = false
    52  				}
    53  			}
    54  			if withChain {
    55  				testPath = filepath.Join(MustGetPathToChainConfig(test.chain), test.part)
    56  			} else {
    57  				testPath = filepath.Join(PathToRootConfig(), test.part)
    58  			}
    59  		} else if test.group == "Cache" {
    60  			os.Setenv("XDG_CACHE_HOME", test.xdg)
    61  			testPath = filepath.Join(PathToCache(test.chain), test.part)
    62  		} else if test.group == "Index" {
    63  			os.Setenv("XDG_CACHE_HOME", test.xdg)
    64  			testPath = filepath.Join(PathToIndex(test.chain), test.part)
    65  		}
    66  
    67  		testPath = strings.Replace(testPath, user.HomeDir, "$HOME", -1)
    68  		if test.expected == "" {
    69  			fmt.Println("")
    70  		} else {
    71  			test.expected = strings.ReplaceAll(test.expected, "{CHAIN}", test.chain)
    72  			if testPath != test.expected {
    73  				test.Report(index, testPath)
    74  				t.Error(index, "Paths don't match (", test.expected, testPath, ")")
    75  			} else {
    76  				test.Report(index, testPath)
    77  			}
    78  		}
    79  	}
    80  }
    81  
    82  func (p *PathTest) Report(index int, got string) {
    83  	fmt.Printf("%2d ", index)
    84  	fmt.Printf("%-7.7s", p.group)
    85  	fmt.Printf("%-5.5s", p.xdg)
    86  	fmt.Printf("%-6.6s", p.os)
    87  	fmt.Printf("%-8.8s", p.chain)
    88  	fmt.Printf("%-12.10s", p.part)
    89  	fmt.Printf("%-50.48s", p.expected)
    90  	fmt.Printf("%s\n", got)
    91  }
    92  
    93  var testSet1 = []PathTest{
    94  	{
    95  		group:    "Config",
    96  		xdg:      "",
    97  		os:       "linux",
    98  		chain:    utils.GetTestChain(),
    99  		part:     "trueBlocks.toml",
   100  		expected: "$HOME/.local/share/trueblocks/trueBlocks.toml",
   101  	},
   102  	{
   103  		group:    "Config",
   104  		xdg:      "xdg",
   105  		os:       "linux",
   106  		chain:    utils.GetTestChain(),
   107  		part:     "blockScrape.toml",
   108  		expected: "$HOME/.local/share/trueblocks/config/{CHAIN}/blockScrape.toml",
   109  	},
   110  	{
   111  		group: "Config",
   112  		xdg:   "/xdg",
   113  		os:    "linux",
   114  		chain: utils.GetTestChain(),
   115  		part:  "trueBlocks.toml",
   116  		// expected: "/xdg/trueBlocks.toml",
   117  		expected: "$HOME/.local/share/trueblocks/trueBlocks.toml",
   118  	},
   119  	{
   120  		group: "Config",
   121  		xdg:   "/xdg",
   122  		os:    "linux",
   123  		chain: utils.GetTestChain(),
   124  		part:  "abis/known-000/uniq_funcs.tab",
   125  		// expected: "/xdg/trueBlocks.toml",
   126  		expected: "$HOME/.local/share/trueblocks/abis/known-000/uniq_funcs.tab",
   127  	},
   128  	{
   129  		group: "Config",
   130  		xdg:   "/xdg",
   131  		os:    "linux",
   132  		chain: "gnosis",
   133  		part:  "blockScrape.toml",
   134  		// expected: "/xdg/config/gnosis/blockScrape.toml",
   135  		expected: "$HOME/.local/share/trueblocks/config/gnosis/blockScrape.toml",
   136  	},
   137  	{
   138  		group:    "Config",
   139  		xdg:      "",
   140  		os:       "darwin",
   141  		chain:    utils.GetTestChain(),
   142  		part:     "blockScrape.toml",
   143  		expected: "$HOME/Library/Application Support/TrueBlocks/config/mainnet/blockScrape.toml",
   144  	},
   145  	{
   146  		group:    "Config",
   147  		xdg:      "xdg",
   148  		os:       "darwin",
   149  		chain:    utils.GetTestChain(),
   150  		part:     "blockScrape.toml",
   151  		expected: "$HOME/Library/Application Support/TrueBlocks/config/{CHAIN}/blockScrape.toml",
   152  	},
   153  	{
   154  		group: "Config",
   155  		xdg:   "/xdg",
   156  		os:    "darwin",
   157  		chain: utils.GetTestChain(),
   158  		part:  "trueBlocks.toml",
   159  		// expected: "/xdg/trueBlocks.toml",
   160  		expected: "$HOME/Library/Application Support/TrueBlocks/trueBlocks.toml",
   161  	},
   162  	{
   163  		group:    "Config",
   164  		xdg:      "",
   165  		os:       "darwin",
   166  		chain:    "gnosis",
   167  		part:     "trueBlocks.toml",
   168  		expected: "$HOME/Library/Application Support/TrueBlocks/trueBlocks.toml",
   169  	},
   170  	{
   171  		group:    "Config",
   172  		xdg:      "xdg",
   173  		os:       "darwin",
   174  		chain:    "gnosis",
   175  		part:     "trueBlocks.toml",
   176  		expected: "$HOME/Library/Application Support/TrueBlocks/trueBlocks.toml",
   177  	},
   178  	{
   179  		group: "Config",
   180  		xdg:   "/xdg",
   181  		os:    "darwin",
   182  		chain: utils.GetTestChain(),
   183  		part:  "trueBlocks.toml",
   184  		// expected: "/xdg/trueBlocks.toml",
   185  		expected: "$HOME/Library/Application Support/TrueBlocks/trueBlocks.toml",
   186  	},
   187  	// {
   188  	// 	group:    "Cache",
   189  	// 	xdg:      "/xdg",
   190  	// 	os:       "linux",
   191  	// 	chain:    "polygon",
   192  	// 	part:     "tx/00/00/",
   193  	// 	expected: "/xdg/cache/{CHAIN}/tx/00/00/",
   194  	// expected: "$HOME/.local/share/trueblocks/cache/{CHAIN}/tx/00/00/",
   195  	// },
   196  	{
   197  		group:    "Cache",
   198  		xdg:      "",
   199  		os:       "darwin",
   200  		chain:    utils.GetTestChain(),
   201  		part:     "abis/0x12.json",
   202  		expected: "$HOME/Library/Application Support/TrueBlocks/cache/{CHAIN}/abis/0x12.json",
   203  		disabled: true,
   204  	},
   205  	{
   206  		group:    "Cache",
   207  		xdg:      "",
   208  		os:       "windows",
   209  		chain:    utils.GetTestChain(),
   210  		part:     "abis/0x12.json",
   211  		expected: "$HOME/Library/Application Support/TrueBlocks/cache/{CHAIN}/abis/0x12.json",
   212  		disabled: true,
   213  	},
   214  	{
   215  		group:    "Index",
   216  		xdg:      "",
   217  		os:       "darwin",
   218  		chain:    utils.GetTestChain(),
   219  		part:     "names.bin",
   220  		expected: "$HOME/Library/Application Support/TrueBlocks/unchained/{CHAIN}/names.bin",
   221  		disabled: true,
   222  	},
   223  }