-
Notifications
You must be signed in to change notification settings - Fork 3
Prepend line directive to copied source files #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
テストを追加してもらえると嬉しいです。
@@ -50,6 +53,14 @@ func WithModules(t *testing.T, srcdir string, modfile io.Reader) (dir string) { | |||
} | |||
|
|||
for _, file := range files { | |||
// Prepend line directive to .go files | |||
if strings.HasSuffix(file.Name(), ".go") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filepath.Ext
を使ってください
// Prepend line directive to .go files | ||
if strings.HasSuffix(file.Name(), ".go") { | ||
fn := filepath.Join(path, file.Name()) | ||
originalFn := strings.TrimPrefix(fn, dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
やりたいことはfilepath.Base
でしょうか?
fn := filepath.Join(path, file.Name()) | ||
originalFn := strings.TrimPrefix(fn, dir) | ||
if err := prependToFile(fn, fmt.Sprintf("//line %s:1\n", originalFn)); err != nil { | ||
t.Fatal("cannot prepend.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もう少し理由をわかりやすく書いてもらえると嬉しいです。
@@ -85,6 +96,50 @@ func WithModules(t *testing.T, srcdir string, modfile io.Reader) (dir string) { | |||
return dir | |||
} | |||
|
|||
func appendFileContent(tmp io.Writer, filename string) error { | |||
f, err := os.OpenFile(filename, os.O_RDONLY, 0600) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.Open
を使わない理由はありますか?
if strings.HasSuffix(file.Name(), ".go") { | ||
fn := filepath.Join(path, file.Name()) | ||
originalFn := strings.TrimPrefix(fn, dir) | ||
if err := prependToFile(fn, fmt.Sprintf("//line %s:1\n", originalFn)); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmt.Sprintf
使うなら直接fmt.Fprintln
でファイルに出力したら良いと思いました。
return nil | ||
} | ||
|
||
func prependToFile(filename string, content string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.Removeはdeferですれば良さそうです。
} | ||
// Write the original file content | ||
if err := appendFileContent(tmp, filename); err != nil { | ||
tmp.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラー処理が必要です。
// Write the original file content | ||
if err := appendFileContent(tmp, filename); err != nil { | ||
tmp.Close() | ||
os.Remove(tmpFilePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラー処理が必要です。
#8 のissueを修正するコードを書いてみました。