Ackee-Blockchain / trident

Rust-based framework to Fuzz and Integration test Solana programs to help you ship secure code.
https://ackee.xyz/trident/docs/latest/
MIT License
188 stars 18 forks source link

:bug: updating gitignore by adding a new line #186 #187

Closed hetdagli234 closed 1 month ago

hetdagli234 commented 1 month ago

A small but important change to the update_gitignore function in the TestGenerator struct.

Modified the writeln! macro call in the update_gitignore function to add the changes in a new line.

st22nestrel commented 1 month ago

@hetdagli234 Could you please adjust the function you made changes in to this :pencil::

#[throws]
    fn update_gitignore(&self, ignored_path: &str) {
        let gitignore_path = construct_path!(self.root, GIT_IGNORE);
        if gitignore_path.exists() {
            let file = File::open(&gitignore_path)?;
            for line in io::BufReader::new(file).lines().map_while(Result::ok) {
                if line == ignored_path {
                    // INFO do not add the ignored path again if it is already in the .gitignore file
                    println!("{SKIP} [{GIT_IGNORE}], already contains [{ignored_path}]");

                    return;
                }
            }
            // Check if the file ends with a newline
            let mut file = File::open(&gitignore_path)?;
            let mut buf = [0; 1];
            file.seek(io::SeekFrom::End(-1))?;
            file.read_exact(&mut buf)?;

            let file = OpenOptions::new().append(true).open(gitignore_path);

            if let Ok(mut file) = file {
                if buf[0] == b'\n' {
                    writeln!(file, "{}", ignored_path)?;
                } else {
                    writeln!(file, "\n{}", ignored_path)?;
                }
                println!("{FINISH} [{GIT_IGNORE}] update with [{ignored_path}]");
            }
        } else {
            println!("{SKIP} [{GIT_IGNORE}], not found")
        }
    }

This change provides consistent output for both cases when:

  1. last entry in .gitignore file ends with EOF
  2. last entry in .gitignore file ends with EOL (and after follows EOF)

With your proposed change there will be an empty line in 2., which we would like to avoid.