Skip to content

Can ctor be used to initialize doctests? #298

@ivan-aksamentov

Description

@ivan-aksamentov

Thank you for the fantastic crate!

I succesfully use ctor in order to initialize my tests, notably I initialize color_eyre, which makes output much prettier:

#[cfg(test)]
mod tests {
  use crate::utils::global_init::global_init;
  use ctor::ctor;

  #[ctor]
  fn init() {
    global_init();
  }
}

However, it does not work for doctests. I tried various permutations of things, none worked - the function is not called. Some of my attempts on Ubuntu 22.04:

#[cfg(doctest)]
mod tests {
  use crate::utils::global_init::global_init;
  use ctor::ctor;

  #[ctor]
  fn init() {
    global_init();
  }
}
#[cfg(any(test, doctest))]
mod tests {
  use crate::utils::global_init::global_init;
  use ctor::ctor;

  #[ctor]
  fn init() {
    global_init();
  }
}
#[cfg(doctest)]
#[ctor::ctor]
fn init() {
  utils::global_init::global_init();
}

Have anyone managed to run ctor with for doctests? Please share your experience.

Or is this something that ctor does not support, perhaps due to technical limitations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions