github.com/mborho/rem@v0.17.0/file_test.go (about)

     1  // rem - A tool to remember things on the command line.
     2  // Copyright (C) 2016 Martin Borho (martin@borho.net)
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	_ "fmt"
    21  	"os"
    22  	"path"
    23  	"path/filepath"
    24  	"regexp"
    25  	"testing"
    26  )
    27  
    28  func removeTestFile(f *os.File) {
    29  	f.Close()
    30  	os.Remove(f.Name())
    31  }
    32  
    33  func TestSetPath(t *testing.T) {
    34  
    35  	file := &File{
    36  		filename: ".rem_test_file",
    37  	}
    38  	err := file.setPath()
    39  	if err != nil {
    40  		t.Error("Error when setting path.")
    41  	}
    42  
    43  	match, _ := regexp.MatchString("/home/[^/]+/"+file.filename, file.filepath)
    44  	if !match {
    45  		t.Errorf("Filepath not in $home: %s", file.filepath)
    46  	}
    47  
    48  	// test with global flag, not non-existant local file
    49  	file.global = false
    50  	err = file.setPath()
    51  	if err != nil {
    52  		t.Error("Error when setting non-exsiting global path.")
    53  	}
    54  
    55  	match, _ = regexp.MatchString("/home/[^/]+/"+file.filename, file.filepath)
    56  	if !match {
    57  		t.Errorf("Local non-existant path not pointing to home dir: %s", file.filepath)
    58  	}
    59  
    60  	// test with existing local file
    61  	f, err := os.Create(".rem_test_file")
    62  	defer removeTestFile(f)
    63  
    64  	err = file.setPath()
    65  	match, _ = regexp.MatchString("/home/.+/"+file.filename+"$", file.filepath)
    66  	if !match {
    67  		t.Errorf("Local non-existant path not pointing to home dir: %s", file.filepath)
    68  	}
    69  }
    70  
    71  func TestTraverseCheckPath(t *testing.T) {
    72  
    73  	file := &File{
    74  		filename: ".rem_test_traversed",
    75  		global:   false,
    76  	}
    77  
    78  	f, err := os.Create(file.filename)
    79  	defer removeTestFile(f)
    80  
    81  	dir, err := os.Getwd()
    82  	if err != nil {
    83  		t.Errorf("Error when creating test dir: %s", dir)
    84  	}
    85  
    86  	testDir := path.Join(dir, "tmp")
    87  	if err := os.Mkdir(testDir, 755); err != nil {
    88  		t.Errorf("Error when creating test dir: %s", testDir)
    89  	}
    90  
    91  	if err := os.Chdir(testDir); err != nil {
    92  		t.Errorf("Error when switching test dir: %s", testDir)
    93  	}
    94  	defer os.Chdir(dir)
    95  
    96  	err = file.setPath()
    97  	if err != nil {
    98  		t.Error("Error when setting path.")
    99  	}
   100  
   101  	if file.filepath != path.Join(dir, file.filename) {
   102  		t.Errorf("Error for traversed filepath: %s", file.filepath)
   103  
   104  	}
   105  
   106  	if err := os.Remove(testDir); err != nil {
   107  		t.Error("Error when removing test dir.")
   108  	}
   109  }
   110  
   111  func TestCreateLocalFile(t *testing.T) {
   112  
   113  	file := &File{
   114  		filename: ".rem_test_create_local_file",
   115  		global:   false,
   116  	}
   117  	file.setPath()
   118  	if err := file.createLocalFile(); err != nil {
   119  		t.Errorf("Error when creating local file.")
   120  	}
   121  
   122  	if _, err := os.Stat(file.filepath); os.IsNotExist(err) {
   123  		t.Errorf("Local file was not created: %s", file.filepath)
   124  	}
   125  	defer removeTestFile(file.file)
   126  
   127  }
   128  
   129  func TestSetFile(t *testing.T) {
   130  
   131  	file := &File{
   132  		filename: ".rem_test_set_file",
   133  		global:   false,
   134  	}
   135  	if err := file.createLocalFile(); err != nil {
   136  		t.Errorf("Error creating local file: %s", file.filepath)
   137  	}
   138  
   139  	if err := file.setFile(false); err != nil {
   140  		t.Errorf("Error opening file: %s", file.filepath)
   141  	}
   142  
   143  	if _, err := os.Stat(file.filepath); os.IsNotExist(err) {
   144  		t.Errorf("Local file was not created: %s", file.filepath)
   145  	}
   146  
   147  	fileInfo, _ := file.file.Stat()
   148  	mode := int(0600)
   149  	if fileInfo.Mode() != os.FileMode(mode) {
   150  		t.Errorf("Wrong perms: %s", fileInfo.Mode())
   151  	}
   152  	defer removeTestFile(file.file)
   153  
   154  }
   155  
   156  func TestSetFileNoAppend(t *testing.T) {
   157  
   158  	file := &File{
   159  		filename: ".rem_test_set_file_no_append",
   160  		global:   false,
   161  	}
   162  	if err := file.createLocalFile(); err != nil {
   163  		t.Errorf("Error creating local file: %s", file.filepath)
   164  	}
   165  
   166  	if err := file.setFile(true); err != nil {
   167  		t.Errorf("Error opening file: %s", file.filepath)
   168  	}
   169  
   170  	if _, err := os.Stat(file.filepath); os.IsNotExist(err) {
   171  		t.Errorf("Local file was not created: %s", file.filepath)
   172  	}
   173  
   174  	fileInfo, _ := file.file.Stat()
   175  	mode := int(0600)
   176  	if fileInfo.Mode() != os.FileMode(mode) {
   177  		t.Errorf("Wrong perms: %s", fileInfo.Mode())
   178  	}
   179  	defer removeTestFile(file.file)
   180  }
   181  
   182  func TestCheckPath(t *testing.T) {
   183  
   184  	file := &File{
   185  		filename: ".rem_test",
   186  		global:   false,
   187  	}
   188  	if err := file.createLocalFile(); err != nil {
   189  		t.Errorf("Error creating local file: %s", file.filepath)
   190  	}
   191  
   192  	dirToCheck := filepath.Dir(file.filepath)
   193  	if exists := file.checkPath(dirToCheck); exists != true {
   194  		t.Errorf("Path not successfully checked: %s", file.filepath)
   195  	}
   196  
   197  	dirToCheck = filepath.Dir(dirToCheck)
   198  	if exists := file.checkPath(dirToCheck); exists != false {
   199  		t.Errorf("Path was successfully checked: %s", file.filepath)
   200  	}
   201  	defer removeTestFile(file.file)
   202  }