this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/filetypes: not a file extension if the file starts with a dot

We'd like to be able to read directories that start with a dot.
Currently the filetypes logic doesn't accept that because `filepath.Ext`
returns the part of the file after the dot even if the dot is at
the start of the file.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I032c77223575cd1dcb6295a3a16d2ca75a43f205
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/542990
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>

+48 -5
+12
cmd/cue/cmd/testdata/script/dotfile.txtar
··· 1 + exec cue export ./.foo 2 + cmp stdout stdout.golden 3 + 4 + -- .foo/x.cue -- 5 + package foo 6 + 7 + x: 1 8 + 9 + -- stdout.golden -- 10 + { 11 + "x": 1 12 + }
+12 -2
internal/filetypes/filetypes.go
··· 102 102 v = v.Fill(b) 103 103 104 104 if b.Encoding == "" { 105 - ext := i.Lookup("extensions", filepath.Ext(b.Filename)) 105 + ext := i.Lookup("extensions", fileExt(b.Filename)) 106 106 if ext.Exists() { 107 107 v = v.Unify(ext) 108 108 } ··· 260 260 if !hasDefault { 261 261 v = v.Unify(i.LookupDef("Default")) 262 262 } 263 - } else if ext := filepath.Ext(filename); ext != "" { 263 + } else if ext := fileExt(filename); ext != "" { 264 264 if x := i.Lookup("extensions", ext); x.Exists() || !hasDefault { 265 265 v = v.Unify(x) 266 266 if err := v.Err(); err != nil { ··· 304 304 305 305 return i, v, nil 306 306 } 307 + 308 + // fileExt is like filepath.Ext except we don't treat file names starting with "." as having an extension 309 + // unless there's also another . in the name. 310 + func fileExt(f string) string { 311 + e := filepath.Ext(f) 312 + if e == "" || e == filepath.Base(f) { 313 + return "" 314 + } 315 + return e 316 + }
+20
internal/filetypes/filetypes_test.go
··· 71 71 Attributes: true, 72 72 }, 73 73 }, { 74 + // Filename starting with a . but no other extension. 75 + name: "filename-with-dot", 76 + in: build.File{ 77 + Filename: ".json", 78 + }, 79 + mode: Def, 80 + out: "#FileInfo.encoding: non-concrete value string", 81 + }, { 74 82 name: "yaml", 75 83 mode: Def, 76 84 in: build.File{ ··· 263 271 out: &build.File{ 264 272 Filename: "file.json", 265 273 Encoding: "json", 274 + Interpretation: "auto", 275 + }, 276 + }, { 277 + in: ".json", 278 + mode: Input, 279 + out: `no encoding specified for file ".json"`, 280 + }, { 281 + in: ".json.yaml", 282 + mode: Input, 283 + out: &build.File{ 284 + Filename: ".json.yaml", 285 + Encoding: "yaml", 266 286 Interpretation: "auto", 267 287 }, 268 288 }, {
+2 -3
internal/filetypes/util.go
··· 15 15 package filetypes 16 16 17 17 import ( 18 - "path/filepath" 19 18 "strings" 20 19 21 20 "cuelang.org/go/cue/ast" ··· 48 47 // Assuming we terminate search for packages once a scoped qualifier is 49 48 // found, we know that any file without an extension (except maybe '-') 50 49 // is invalid. We can therefore assume it is a package. 51 - // The section may still contain a dot, for instance ./foo/. or ./foo/... 52 - return strings.TrimLeft(filepath.Ext(s), ".") == "" 50 + // The section may still contain a dot, for instance ./foo/., ./.foo/, or ./foo/... 51 + return strings.TrimLeft(fileExt(s), ".") == "" 53 52 54 53 // NOTE/TODO: we have not needed to check whether it is an absolute package 55 54 // or whether the package starts with a dot. Potentially we could thus relax
+2
internal/filetypes/util_test.go
··· 27 27 {".../foo", true}, 28 28 {"./:foo", true}, 29 29 {"foo.bar/foo", true}, 30 + {"./.foo", true}, 31 + {"./.foo.json", false}, 30 32 31 33 // Not supported yet, but could be and isn't anything else valid. 32 34 {":foo", true},