Skip to content

Commit

Permalink
output: Identify extension-less text types as text
Browse files Browse the repository at this point in the history
See #3614
  • Loading branch information
bep committed Jun 20, 2017
1 parent 19f2e72 commit c43b512
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion output/outputFormat.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ func (formats Formats) FromFilename(filename string) (f Format, found bool) {
}

if ext != "" {
return formats.GetBySuffix(ext)
f, found = formats.GetBySuffix(ext)
if !found && len(parts) == 2 {
// For extensionless output formats (e.g. Netlify's _redirects)
// we must fall back to using the extension as format lookup.
f, found = formats.GetByName(ext)
}
}
return
}
Expand Down
41 changes: 41 additions & 0 deletions output/outputFormat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,47 @@ func TestGetFormatByExt(t *testing.T) {
require.False(t, found)
}

func TestGetFormatByFilename(t *testing.T) {
noExtNoDelimMediaType := media.TextType
noExtNoDelimMediaType.Suffix = ""
noExtNoDelimMediaType.Delimiter = ""

noExtMediaType := media.TextType
noExtMediaType.Suffix = ""

var (
noExtDelimFormat = Format{
Name: "NEM",
MediaType: noExtNoDelimMediaType,
BaseName: "_redirects",
}
noExt = Format{
Name: "NEX",
MediaType: noExtMediaType,
BaseName: "next",
}
)

formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat}
f, found := formats.FromFilename("my.amp.html")
require.True(t, found)
require.Equal(t, AMPFormat, f)
f, found = formats.FromFilename("my.ics")
require.True(t, found)
f, found = formats.FromFilename("my.html")
require.True(t, found)
require.Equal(t, HTMLFormat, f)
f, found = formats.FromFilename("my.nem")
require.True(t, found)
require.Equal(t, noExtDelimFormat, f)
f, found = formats.FromFilename("my.nex")
require.True(t, found)
require.Equal(t, noExt, f)
f, found = formats.FromFilename("my.css")
require.False(t, found)

}

func TestDecodeFormats(t *testing.T) {

mediaTypes := media.Types{media.JSONType, media.XMLType}
Expand Down

0 comments on commit c43b512

Please sign in to comment.