func TestReadOnly(t *testing.T) { m := map[string]string{"x": "y"} rfs := mapfs.New(m) wfs := rwvfs.ReadOnly(rfs) if _, err := rfs.Stat("/x"); err != nil { t.Error(err) } _, err := wfs.Create("/y") if want := (&os.PathError{"create", "/y", rwvfs.ErrReadOnly}); !reflect.DeepEqual(err, want) { t.Errorf("Create: got err %v, want %v", err, want) } err = wfs.Mkdir("/y") if want := (&os.PathError{"mkdir", "/y", rwvfs.ErrReadOnly}); !reflect.DeepEqual(err, want) { t.Errorf("Mkdir: got err %v, want %v", err, want) } err = wfs.Remove("/y") if want := (&os.PathError{"remove", "/y", rwvfs.ErrReadOnly}); !reflect.DeepEqual(err, want) { t.Errorf("Remove: got err %v, want %v", err, want) } }
// Map returns a new FileSystem from the provided map. Map keys should be // forward slash-separated pathnames and not contain a leading slash. func Map(m map[string]string) FileSystem { fs := mapFS{ m: m, dirs: map[string]struct{}{"/": struct{}{}}, mu: new(sync.RWMutex), FileSystem: mapfs.New(m), } // Create initial dirs. for path := range m { if err := MkdirAll(fs, filepath.Dir(path)); err != nil { panic(err.Error()) } } return fs }