
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
github.com/aspose-words-cloud/aspose-words-cloud-go/v2202
/*
*/
package api_test
import ( "bufio" "bytes" "fmt" "io" "os" "path" "path/filepath" "regexp" "runtime" "testing"
"github.com/aspose-words-cloud/aspose-words-cloud-go/v2202/api"
"github.com/aspose-words-cloud/aspose-words-cloud-go/v2202/api/models"
)
func TestReadmeCode(t *testing.T) {
localFilePath := commonTestFile
remoteFolder := path.Join(remoteBaseTestDataFolder, "ReadmeCode")
remoteName := "ReadmeCode.docx"
remotePath := path.Join(remoteFolder, remoteName)
configFilePath := GetConfigFilePath()
// Start README example
// init words cloud api
config, _ := models.NewConfiguration(configFilePath)
wordsApi, ctx, _ := api.CreateWordsApi(config)
// upload test.docx to a cloud
// remote.docx is a name in the cloud
file, _ := os.Open(localFilePath)
uploadRequest := &models.UploadFileRequest{
FileContent: file,
Path: &remotePath,
}
wordsApi.UploadFile(ctx, uploadRequest)
// get a text for the first paragraph of the first section
options := map[string]interface{}{
"folder": remoteFolder,
}
request := &models.GetParagraphsRequest{
Name: &remoteName,
Optionals: options,
}
result, _, _ := wordsApi.GetParagraphs(ctx, request)
fmt.Println(result.Paragraphs.ParagraphLinkList[0].Text)
// End README example
writeToReadme(t)
}
func writeToReadme(t testing.T) { // set regexes startPatern := regexp.MustCompile("^\s// Start README example\s*$") endPattern := regexp.MustCompile("^\s*// End README example\s*$")
// set paths
_, filename, _, _ := runtime.Caller(0)
testFolder := filepath.Dir(filename)
sourcePath := filepath.Join(testFolder, "readme_test.go")
readmePath := filepath.Join(testFolder, "../../README.md")
// read the file
codeLines, err := readLines(sourcePath)
if err != nil {
t.Error(err)
}
// extract example code
var readmeCode []string
skipMode := true
for i := 0; i < len(codeLines); i++ {
if skipMode {
skipMode = !startPatern.Match([]byte(codeLines[i]))
}
if !skipMode {
readmeCode = append(readmeCode, codeLines[i])
skipMode = endPattern.Match([]byte(codeLines[i]))
}
}
if len(readmeCode) < 2 {
t.Error("Code has not found in readme_test.go")
}
// read readme.md
readmeLines, err := readLines(readmePath)
if err != nil {
t.Error(err)
}
// replace example code
var newReadmeLines []string
codeMode := false
for i := 0; i < len(readmeLines); i++ {
if !codeMode {
codeMode = startPatern.Match([]byte(readmeLines[i]))
if codeMode {
newReadmeLines = append(newReadmeLines, readmeCode...)
}
}
if codeMode {
codeMode = !endPattern.Match([]byte(readmeLines[i]))
continue
}
if !codeMode {
newReadmeLines = append(newReadmeLines, readmeLines[i])
}
}
// write new readme
err = writeLines(newReadmeLines, readmePath)
if err != nil {
t.Error(err)
}
}
// Read a whole file into the memory and store it as array of lines func readLines(path string) (lines []string, err error) { var ( file *os.File part []byte prefix bool ) if file, err = os.Open(path); err != nil { return } defer file.Close()
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 0))
for {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
lines = append(lines, buffer.String())
buffer.Reset()
}
}
if err == io.EOF {
err = nil
}
return
}
func writeLines(lines []string, path string) (err error) { var ( file *os.File )
if file, err = os.Create(path); err != nil {
return
}
defer file.Close()
for _, item := range lines {
_, err := file.WriteString(item + "\n")
if err != nil {
break
}
}
return
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.