func setupTestBag(bagName string) (*bagins.Bag, error) { bag, err := bagins.NewBag(os.TempDir(), bagName, []string{"md5"}, false) if err != nil { return nil, err } return bag, nil }
// Setups up a bag with some custom tag files. func setupCustomBag(bagName string) (*bagins.Bag, error) { bag, err := bagins.NewBag(os.TempDir(), bagName, []string{"md5", "sha256"}, true) if err != nil { return nil, err } bag.AddTagfile("bag-info.txt") bagInfo, _ := bag.TagFile("bag-info.txt") bagInfo.Data.SetFields([]bagins.TagField{ *bagins.NewTagField("Source-Organization", "APTrust"), *bagins.NewTagField("Bagging-Date", "2016-06-01"), *bagins.NewTagField("Bag-Count", "1"), *bagins.NewTagField("Internal-Sender-Description", "This is a test bag with no content."), *bagins.NewTagField("Internal-Sender-Identification", "Bag XYZ"), }) bag.AddTagfile("aptrust-info.txt") aptrustInfo, _ := bag.TagFile("aptrust-info.txt") aptrustInfo.Data.SetFields([]bagins.TagField{ *bagins.NewTagField("Title", "APTrust Generic Test Bag"), *bagins.NewTagField("Rights", "Consortia"), }) errors := bag.Save() if errors != nil && len(errors) > 0 { return nil, errors[0] } return bag, nil }
func TestNewBag(t *testing.T) { // It should raise an error if the destination dir does not exist. badLocation := filepath.Join(os.TempDir(), "/GOTESTNOT_EXISTs/") _, err := bagins.NewBag(badLocation, "_GOFAILBAG_", []string{"md5"}, false) if err == nil { t.Error("NewBag function does not recognize when a directory does not exist!") } // It should raise an error if the bag already exists. os.MkdirAll(filepath.Join(badLocation, "_GOFAILBAG_"), 0766) defer os.RemoveAll(badLocation) _, err = bagins.NewBag(badLocation, "_GOFAILBAG_", []string{"md5"}, false) if err == nil { t.Error("Error not thrown when bag already exists as expected.") } // It should create a bag without any errors. bagName := "_GOTEST_NEWBAG_" bag, err := setupTestBag("_GOTEST_NEWBAG_") defer os.RemoveAll(bag.Path()) // It should find all of the following files and directories. if _, err = os.Stat(filepath.Join(os.TempDir(), bagName)); os.IsNotExist(err) { t.Error("Bag directory does not exist!") } if data, err := os.Stat(filepath.Join(os.TempDir(), bagName, "data")); os.IsNotExist(err) || !data.IsDir() { t.Error("Data directory does not exist or is not a directory!") } if _, err = os.Stat(filepath.Join(bag.Path(), "bagit.txt")); os.IsNotExist(err) { bi, err := bag.BagInfo() if err != nil { t.Error(err) } t.Errorf("bagit.txt does not exist! %s", bi.Name()) } if _, err = os.Stat(filepath.Join(os.TempDir(), bagName, "manifest-md5.txt")); os.IsNotExist(err) { t.Error("manifest-md5.txt does not exist!") } }
func main() { if dir == "" { usage() return } if name == "" { usage() return } if payload == "" { usage() return } algoList := parseAlgorithms(algo) createTagManifests := false if tagmanifests == "true" { createTagManifests = true } begin := time.Now() bag, err := bagins.NewBag(dir, name, algoList, createTagManifests) if err != nil { fmt.Println("Bag Error:", err) return } errs := bag.AddDir(payload) for idx := range errs { fmt.Println("AddDir Error:", errs[idx]) return } bag.Save() elapsed := time.Since(begin) fmt.Println("END: elapsed in", elapsed.Seconds(), "seconds.") return }